You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.9 KiB

8 months ago
package com.dsideal.base.Tools.Test;
8 months ago
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
8 months ago
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
8 months ago
import java.util.ArrayList;
8 months ago
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class TestOutSideExcel {
/**
*
*
* @throws IOException
*/
8 months ago
public static void UnCompress(String wordPath, String workingPath) throws IOException {
8 months ago
workingPath = workingPath.replace("\\", "/");
if (!workingPath.endsWith("/")) workingPath += "/";
8 months ago
File file = new File(wordPath);//取得word文件
8 months ago
FileInputStream inputStream = new FileInputStream(file);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry entry;
8 months ago
byte[] ch = new byte[256];
8 months ago
while ((entry = zipInputStream.getNextEntry()) != null) {
8 months ago
File zFile = new File(workingPath + entry.getName());
8 months ago
if (entry.isDirectory()) {
if (!zFile.exists()) {
zFile.mkdirs();
}
zipInputStream.closeEntry();
} else {
File fpath = new File(zFile.getParent());
if (!fpath.exists()) {
fpath.mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(zFile);
int i;
while ((i = zipInputStream.read(ch)) != -1) {
outputStream.write(ch, 0, i);
}
zipInputStream.closeEntry();
outputStream.close();
}
}
inputStream.close();
}
8 months ago
//折线图
public static String LINE = "lineChart";
//柱状图
public static String BAR = "barChart";
8 months ago
8 months ago
/**
* Chart
*
* @param workingPath
* @param chartNumber
*/
public static List<List<String>> readChar(String workingPath, int chartNumber, String type) throws DocumentException {
List<List<String>> res = new ArrayList<>();
8 months ago
String xml = workingPath + "\\word\\charts\\chart" + chartNumber + ".xml";
if (!(new File(xml).exists())) {
System.out.println("没有找到第" + chartNumber + "个图表");
8 months ago
return res;
8 months ago
}
//3、开始读取
8 months ago
// 创建 SAXReader 对象,读取 XML 文件
8 months ago
SAXReader reader = new SAXReader();
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<Element> xList = root.element("chart").element("plotArea")
8 months ago
.element(type).element("ser").element("cat")
8 months ago
.element("numRef").element("numCache").elements("pt");
8 months ago
///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache
List<Element> yList = root.element("chart").element("plotArea")
8 months ago
.element(type).element("ser").element("val")
8 months ago
.element("numRef").element("numCache").elements("pt");
8 months ago
for (int i = 0; i < xList.size(); i++) {
List<String> row = new ArrayList<>();
row.add(xList.get(i).element("v").getText());
row.add(yList.get(i).element("v").getText());
res.add(row);
8 months ago
}
8 months ago
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<List<String>> list1 = readChar(workingPath, chartNumber, LINE);
ExcelKit.printTable(list1);
System.out.println("=========================================================");
//读取第二个图表
chartNumber = 2;
List<List<String>> list2 = readChar(workingPath, chartNumber, BAR);
ExcelKit.printTable(list2);
8 months ago
}
}