main
黄海 8 months ago
parent 2f394cd00d
commit 2b1f1efb72

@ -360,24 +360,15 @@ public class ExcelKit {
* @throws IOException
* @throws InvalidFormatException
*/
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) 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());
if (charts.size() < chartNumber + 1) {
System.out.println(docPath + ",没有第" + (chartNumber + 1) + "个图表,请检查!");
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, InvalidFormatException, InterruptedException, DocumentException {
List<List<String>> data = readChart(docPath, chartNumber);
//需要跳过前skipRowCount行先要判断一下是不是够跳的不够跳的直接输出错误
if (data.size() < skipRowCount) {
System.out.println("skipRowCount is too big, please check your data.");
return null;
}
XSSFWorkbook workbook = charts.get(chartNumber).getWorkbook();
List<List<String>> data = ExcelKit.readSheet(workbook, skipRowCount);
is.close();
if (data.isEmpty()) {
System.out.println(docPath + ",第" + (chartNumber + 1) + "个图表,存在数据外链接情况,请人为处理!");
// System.exit(-1);
}
//截取部分数据返回
data = data.subList(skipRowCount, data.size());
return data;
}
@ -492,51 +483,6 @@ public class ExcelKit {
}
}
/**
* 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();
}
}
public static void ReadChart(String sourceWord) throws IOException, InvalidFormatException {
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();
System.out.println(workbook.getNumberOfSheets());
XSSFSheet sheet = workbook.getSheetAt(0);
System.out.println("序号=" + (i + 1) + ",sheet名称=" + sheet.getSheetName());
if (sheet.getPhysicalNumberOfRows() == 0) {
System.out.println("===第" + (i + 1) + "个图表需要人工编辑一下Sheet名称否则无法读取===");
System.exit(0);
}
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(ExcelKit.readCell(cell) + "\t");
}
System.out.println();
}
}
//关闭流
is.close();
}
/**
*

@ -8,6 +8,7 @@ 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.dom4j.DocumentException;
import java.io.*;
import java.util.List;
@ -15,8 +16,9 @@ import java.util.List;
public class TestSingle {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, DocumentException {
String sourceWord = "c:/西双版纳州人口变化及其对教育的影响20240420.docx";
ExcelKit.ReadChart(sourceWord);
List<List<String>> list = ExcelKit.readChart(sourceWord, 1);
ExcelKit.printTable(list);
}
}

Loading…
Cancel
Save