main
黄海 8 months ago
parent 7df422bc9a
commit 10edfd7e73

@ -15,18 +15,10 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ExcelKit {
//与python交互使用的excel文件路径
public static String excelPath = "c:/task.xlsx";
//执行的python路径这里我使用的是anaconda3的python路径自行修改,注意要在这个环境中pip安装了python-docx否则会报错
public static String python = "D:\\anaconda3\\envs\\py310\\python.exe";
//python脚本路径
public static String py = "D:\\dsWork\\YunNanDsBase\\Py\\TuBiao.py";
/**
* xlsxlsx
*
@ -142,10 +134,6 @@ public class ExcelKit {
return readSheet(new XSSFWorkbook(new FileInputStream(sourceExcel)), skipRowCount);
}
public static List<List<String>> readSheet(XSSFWorkbook workbook) throws IOException {
return readSheet(workbook, 1);
}
public static List<List<String>> readSheet(XSSFWorkbook workbook, int skipRowCount) throws IOException {
XSSFSheet sheet = workbook.getSheetAt(0);
List<List<String>> array = new ArrayList<>();
@ -353,109 +341,6 @@ public class ExcelKit {
}
return --rlt;
}
/**
* python+comWORD
*
* @throws IOException
* @throws InterruptedException
*/
public static void callPythonRead() throws IOException, InterruptedException {
delExcel(excelPath);
// 创建ProcessBuilder对象并设置Python脚本的路径
ProcessBuilder processBuilder = new ProcessBuilder(python, py);
// 重定向错误流到标准输出这样可以在Java中捕获所有的输出
processBuilder.redirectErrorStream(true);
// 启动进程
Process process = processBuilder.start();
// 读取Python脚本的输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 等待Python脚本执行完成
process.waitFor();
}
public static void callPythonPrepare(String docPath, int tuBiaoNum) throws IOException {
String taskTxt = "c:/task.txt";
//如果文件存在则删除
if (new File(taskTxt).exists()) {
new File(taskTxt).delete();
}
//将docPath写入taskTxt,第一行
//将tuBiaoNum写入taskTxt,第二行
FileWriter writer = new FileWriter(taskTxt, StandardCharsets.UTF_8);
writer.write(docPath);
writer.write("\n");
writer.write(String.valueOf(tuBiaoNum));
writer.write("\n");
writer.flush();
writer.close();
}
/**
* excel
*
* @param excelFilePath
* @param skipRows
* @return
*/
public static List<List<String>> readExcelToList(String excelFilePath, int skipRows) {
List<List<String>> data = new ArrayList<>();
try (FileInputStream inputStream = new FileInputStream(excelFilePath)) {
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
// 创建一个迭代器来遍历行,跳过指定数量的行
Iterator<Row> rowIterator = sheet.iterator();
while (skipRows > 0 && rowIterator.hasNext()) {
rowIterator.next();
skipRows--;
}
// 遍历每一行
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
List<String> rowData = new ArrayList<>();
// 遍历每一行中的每一列
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// 根据单元格的不同类型获取数据
switch (cell.getCellType()) {
case STRING:
rowData.add(cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
rowData.add(cell.getDateCellValue().toString());
} else {
rowData.add(Double.toString(cell.getNumericCellValue()));
}
break;
case BOOLEAN:
rowData.add(Boolean.toString(cell.getBooleanCellValue()));
break;
case FORMULA:
rowData.add(cell.getCellFormula());
break;
default:
rowData.add("");
break;
}
}
data.add(rowData);
}
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
/**
*
*
@ -581,6 +466,10 @@ public class ExcelKit {
return dataList;
}
/**
*
* @param dataList
*/
public static void printTable(List<List<String>> dataList) {
for (List<String> row : dataList) {
for (String cell : row) {
@ -589,4 +478,17 @@ public class ExcelKit {
System.out.println();
}
}
/**
* Sheet
* @param sheet
*/
public static void printSheet(XSSFSheet sheet) {
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(ExcelKit.readCell(cell) + "\t");
}
System.out.println();
}
}
}

@ -17,11 +17,6 @@ public class TestCallPython {
//Excel文件生成位置
String excelPath = "c:/task.xlsx";
//写入交互文本文件
ExcelKit.callPythonPrepare(docPath, tuBiaoNum);
//对图表进行读取
ExcelKit.delExcel(excelPath);
ExcelKit.callPythonRead();
//读取生成的EXCEL,使用POI就可以了
List<List<String>> sheetList = ExcelKit.readSheet(excelPath, 1);
System.out.println(sheetList);

@ -0,0 +1,55 @@
package com.dsideal.base.Tools.FillData.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMulti {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException {
String path = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
//获取此目录下所有的文件需要以docx结尾并且不能以~开头
File file = new File(path);
File[] files = file.listFiles();
for (File f : files) {
if (f.getName().endsWith(".docx") && !f.getName().startsWith("~")) {
String sourceWord = f.getAbsolutePath();
InputStream is = new FileInputStream(sourceWord);
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
//直接输出图表数量
System.out.println("doc.getCharts=" + doc.getCharts().size());
List<XWPFChart> chartList = doc.getCharts();
//排序后的图表
chartList = ExcelKit.getSortListForXWPFChart(chartList);
//遍历输出
for (int i = 0; i < chartList.size(); i++) {
XWPFChart chart = chartList.get(i);
XSSFWorkbook workbook = chart.getWorkbook();
XSSFSheet sheet = workbook.getSheetAt(0);
if (sheet.getPhysicalNumberOfRows() == 0) {
System.out.println(sourceWord + ",第" + (i + 1) + "个图表需要人工编辑一下Sheet名称否则无法读取===");
System.exit(0);
}
//打印看看
ExcelKit.printSheet(sheet);
}
//关闭流
is.close();
}
}
}
}

@ -8,65 +8,23 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TestSingle {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, Docx4JException {
String sourceWord = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510\\昭通市人口变化及其对教育的影响20240416.docx";
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException {
String sourceWord="D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510\\西双版纳州人口变化及其对教育的影响20240420.docx";
InputStream is = new FileInputStream(sourceWord);
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
//收集图表
Set<XWPFChart> chartSet = new HashSet<>();
for (IBodyElement element : doc.getBodyElements()) {
//直白的图表
if (element instanceof XWPFChart chart) {
chartSet.add(chart);
}
//段落
if (element instanceof XWPFParagraph) {
for (XWPFRun run : ((XWPFParagraph) element).getRuns()) {
for (XWPFChart chart : run.getDocument().getCharts()) {
if (chart != null) {
chartSet.add(chart);
}
}
}
}
if (element instanceof XWPFTable) {
XWPFTable table = (XWPFTable) element;
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (IBodyElement cellElement : cell.getBodyElements()) {
if (cellElement instanceof XWPFChart) {
XWPFChart chart = (XWPFChart) cellElement;
chartSet.add(chart);
}
}
}
}
}
if (element instanceof XWPFHeader || element instanceof XWPFFooter) {
for (IBodyElement headerElement : ((XWPFHeaderFooter) element).getBodyElements()) {
if (headerElement instanceof XWPFChart) {
XWPFChart chart = (XWPFChart) headerElement;
chartSet.add(chart);
}
}
}
}
System.out.println("图表个数=" + chartSet.size());
//直接输出图表数量
System.out.println("doc.getCharts=" + doc.getCharts().size());
List<XWPFChart> chartList = doc.getCharts();
//排序后的图表
List<XWPFChart> chartList = new ArrayList<>(chartSet);
chartList = ExcelKit.getSortListForXWPFChart(chartList);
//遍历输出
for (int i = 0; i < chartList.size(); i++) {
XWPFChart chart = chartList.get(i);
@ -75,6 +33,7 @@ public class TestSingle {
System.out.println("序号=" + (i + 1) + ",sheet名称=" + sheet.getSheetName());
if (sheet.getSheetName().equals("Sheet0")) {
System.out.println("===第" + (i + 1) + "个图表需要人工编辑一下Sheet名称否则无法读取===");
System.exit(0);
}
for (Row row : sheet) {
for (Cell cell : row) {

Loading…
Cancel
Save