diff --git a/src/main/java/com/dsideal/base/Tools/Test/TestOutSideExcel.java b/src/main/java/com/dsideal/base/Tools/Test/TestOutSideExcel.java index 77fddda6..c2c8b994 100644 --- a/src/main/java/com/dsideal/base/Tools/Test/TestOutSideExcel.java +++ b/src/main/java/com/dsideal/base/Tools/Test/TestOutSideExcel.java @@ -1,5 +1,6 @@ package com.dsideal.base.Tools.Test; +import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.dom4j.Document; import org.dom4j.DocumentException; @@ -13,6 +14,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -24,6 +26,8 @@ public class TestOutSideExcel { * @throws IOException */ public static void UnCompress(String wordPath, String workingPath) throws IOException { + workingPath = workingPath.replace("\\", "/"); + if (!workingPath.endsWith("/")) workingPath += "/"; File file = new File(wordPath);//取得word文件 FileInputStream inputStream = new FileInputStream(file); ZipInputStream zipInputStream = new ZipInputStream(inputStream); @@ -53,18 +57,23 @@ public class TestOutSideExcel { inputStream.close(); } - public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException { - String sourceDoc = "c:/西双版纳州人口变化及其对教育的影响20240420.docx"; + //折线图 + public static String LINE = "lineChart"; + //柱状图 + public static String BAR = "barChart"; - //1、将word文件解压缩 - String workingPath = "C:\\zipFile"; - UnCompress(sourceDoc, workingPath); - //2、我们需要第几个图表 - int chartNumber = 1; + /** + * 读取指定的Chart数据 + * + * @param workingPath + * @param chartNumber + */ + public static List> readChar(String workingPath, int chartNumber, String type) throws DocumentException { + List> res = new ArrayList<>(); String xml = workingPath + "\\word\\charts\\chart" + chartNumber + ".xml"; if (!(new File(xml).exists())) { System.out.println("没有找到第" + chartNumber + "个图表"); - return; + return res; } //3、开始读取 // 创建 SAXReader 对象,读取 XML 文件 @@ -72,24 +81,44 @@ public class TestOutSideExcel { Document document = reader.read(new File(xml)); // 获取根元素 Element root = document.getRootElement(); - //折线图 //将xml用IDEA打开,搜索关键的数据值,然后右键查看XPATH完整路径可以获取到下面的路径 ///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:cat/c:numRef/c:numCache/c:pt/c:v List xList = root.element("chart").element("plotArea") - .element("lineChart").element("ser").element("cat") + .element(type).element("ser").element("cat") .element("numRef").element("numCache").elements("pt"); - for (Element e : xList) { - System.out.println(e.element("v").getText()); - } ///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache List yList = root.element("chart").element("plotArea") - .element("lineChart").element("ser").element("val") + .element(type).element("ser").element("val") .element("numRef").element("numCache").elements("pt"); - for (Element e : yList) { - System.out.println(e.element("v").getText()); + for (int i = 0; i < xList.size(); i++) { + List row = new ArrayList<>(); + row.add(xList.get(i).element("v").getText()); + row.add(yList.get(i).element("v").getText()); + res.add(row); } + return res; + } + + public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException { + String sourceDoc = "c:/西双版纳州人口变化及其对教育的影响20240420.docx"; + + //1、将word文件解压缩 + String workingPath = "C:\\zipFile"; + UnCompress(sourceDoc, workingPath); + //2、我们需要第几个图表 + int chartNumber = 1; + //读取第一个图表 + List> list1 = readChar(workingPath, chartNumber, LINE); + ExcelKit.printTable(list1); + + System.out.println("========================================================="); + + //读取第二个图表 + chartNumber = 2; + List> list2 = readChar(workingPath, chartNumber, BAR); + ExcelKit.printTable(list2); } }