|
|
|
@ -2,16 +2,20 @@ package com.dsideal.base.Tools.FillData.ExcelKit;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.base.DataEase.Model.ExcelReader;
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
|
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
|
|
|
|
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.*;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ExcelKit {
|
|
|
|
@ -380,4 +384,104 @@ public class ExcelKit {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定文档中指定图表数据
|
|
|
|
|
*
|
|
|
|
|
* @param docPath 文档路径
|
|
|
|
|
* @param chartNumber 图表序号
|
|
|
|
|
* @param skipRowCount 跳过的行数
|
|
|
|
|
* @return 结果数据
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws InvalidFormatException
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount, int expectLimit) throws IOException, InvalidFormatException, InterruptedException {
|
|
|
|
|
InputStream is = new FileInputStream(docPath);
|
|
|
|
|
ZipSecureFile.setMinInflateRatio(-1.0d);
|
|
|
|
|
XWPFDocument doc = new XWPFDocument(is);
|
|
|
|
|
//排序后的图表
|
|
|
|
|
List<XWPFChart> charts = ExcelKit.getSortListForXWPFChart(doc.getCharts());
|
|
|
|
|
XSSFWorkbook workbook = charts.get(chartNumber).getWorkbook();
|
|
|
|
|
List<List<String>> data = ExcelKit.readSheet(workbook, skipRowCount);
|
|
|
|
|
is.close();
|
|
|
|
|
|
|
|
|
|
//如果达到目标预期的数量,就直接返回poi获取的数据列表
|
|
|
|
|
int totalRow = data.size() + skipRowCount;
|
|
|
|
|
if (totalRow < expectLimit) {
|
|
|
|
|
System.out.println("期望数量>=" + expectLimit + ",现在只有" + totalRow + "条,理解为POI读取WORD图表存在问题,使用python进行二次获取数据...");
|
|
|
|
|
// 留出足够的com关闭word的时间长度,否则会有异常
|
|
|
|
|
Thread.sleep(4000);
|
|
|
|
|
//否则调用python+com进行再次获取数据列表,这次获取的可能才是对的
|
|
|
|
|
//写入交互文本文件
|
|
|
|
|
ExcelKit.callPythonPrepare(docPath, chartNumber);
|
|
|
|
|
//对图表进行读取
|
|
|
|
|
ExcelKit.callPythonRead();
|
|
|
|
|
//读取生成的EXCEL,使用POI就可以了
|
|
|
|
|
//使用POI
|
|
|
|
|
int skipRows = 1; // 假设我们要跳过第一行
|
|
|
|
|
data = ExcelKit.readExcelToList(ExcelKit.excelPath, skipRows);
|
|
|
|
|
System.out.println("二次获取数据条目数量:" + data.size() + ",期望数量=" + expectLimit);
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|