main
黄海 8 months ago
parent e4fb61c35f
commit 97c101634b

@ -102,6 +102,7 @@ public class A8 {
for (List<String> row : source) {
String year = row.getFirst().split("\\.")[0];
year = year.replace("年", "");
String q = row.get(1);
if (StrKit.isBlank(q)) {

@ -4,12 +4,15 @@ import cn.hutool.core.io.FileUtil;
import com.dsideal.base.DataEase.Model.ExcelReader;
import com.jfinal.kit.StrKit;
import org.apache.commons.io.FileUtils;
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 org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
@ -353,10 +356,11 @@ public class ExcelKit {
* @return
* @throws IOException
*/
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException {
List<List<String>> data = readChart(docPath, chartNumber);
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException, InterruptedException, InvalidFormatException {
ExcelKit.UnCompress(docPath);
List<List<String>> data = readChartByXml(docPath, chartNumber);
//需要跳过前skipRowCount行先要判断一下是不是够跳的不够跳的直接输出错误
if (data == null || data.size() < skipRowCount) {
if (data.size() < skipRowCount) {
return null;
}
//截取部分数据返回
@ -520,7 +524,7 @@ public class ExcelKit {
*
* @param chartNumber
*/
public static List<List<String>> readChart(String sourceDoc, int chartNumber) throws DocumentException, IOException {
public static List<List<String>> readChartByXml(String sourceDoc, int chartNumber) throws DocumentException, IOException {
//将word文件解压缩
UnCompress(sourceDoc);
List<List<String>> matrix = new ArrayList<>();
@ -529,7 +533,7 @@ public class ExcelKit {
System.out.println(sourceDoc + " 没有找到第" + chartNumber + "个图表");
return matrix;
}
return readChart(xml);
return readChartByXml(xml);
}
/**
@ -560,7 +564,7 @@ public class ExcelKit {
* @return
* @throws DocumentException
*/
public static List<List<String>> readChart(String xmlPath) throws DocumentException {
public static List<List<String>> readChartByXml(String xmlPath) throws DocumentException {
//图表类型
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
@ -574,9 +578,14 @@ public class ExcelKit {
List<Element> children = root.element("chart").element("plotArea").elements();//工作区
boolean hadReadXList = false;//是不是已经读取过横坐标也就是年份的数据了
for (Element child : children) {
//System.out.println("当前遍历到的元素是:" + child.getName());
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);//将CHART_TYPES数组转换为List
if (child.getName().endsWith("Chart") && !CHART_TYPES_LIST.contains(child.getName())) {
System.out.println("发现除线形图和柱状图以外的图表类型,本系统不支持这种类型,请人为修改:" + child.getName());
System.exit(-1);
}
if (CHART_TYPES_LIST.contains(child.getName())) {//如果当前遍历到的子元素是折线图或者柱状图
//System.out.println("找到图表类型:" + child.getName());
String type = child.getName();
//ce:chart element
Element ce = root.element("chart").element("plotArea").element(type);
@ -664,4 +673,32 @@ public class ExcelKit {
if (s.contains("百")) return 100;
return 1;
}
/**
* POI
*
* @param docPath
* @param chartNumber
* @param skipRowCount
* @return
* @throws IOException
* @throws DocumentException
* @throws InterruptedException
* @throws InvalidFormatException
*/
public static List<List<String>> readChartByPOI(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException, InterruptedException, InvalidFormatException {
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) {
System.out.println(docPath + ",没有第" + chartNumber + "个图表,请检查!");
System.exit(-1);
}
XSSFWorkbook workbook = charts.get(chartNumber - 1).getWorkbook();
List<List<String>> data = ExcelKit.readSheet(workbook, skipRowCount);
is.close();
return data;
}
}

@ -13,9 +13,8 @@ import java.util.List;
public class TestOutSideExcel {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException {
String sourceDoc = "c:/嵩明县人口变化趋势对基础教育的影响.docx";
String sourceDoc = "c:/《武定县人口变化及其对教育影响的报告》.docx";
ExcelKit.UnCompress(sourceDoc);
//需要第几个图表
for (int chartNumber = 1; chartNumber <= 1; chartNumber++) {
System.out.println("正在处理第" + chartNumber + "个图表的信息~");

@ -1,6 +1,7 @@
package com.dsideal.base.Tools.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.dom4j.DocumentException;
import java.io.IOException;
@ -9,7 +10,7 @@ import java.util.*;
public class TestXml {
public static void main(String[] args) throws DocumentException, IOException {
public static void main(String[] args) throws DocumentException, IOException, InterruptedException, InvalidFormatException {
String sourceDoc = "c:/西双版纳州人口变化及其对教育的影响20240420.docx";
List<List<String>> res = ExcelKit.getChartData(sourceDoc, 1,0);
ExcelKit.printTable(res);

Loading…
Cancel
Save