|
|
|
@ -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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将xls转换为xlsx
|
|
|
|
|
*
|
|
|
|
@ -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+com读取WORD中的图表
|
|
|
|
|
*
|
|
|
|
|
* @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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|