|
|
|
@ -4,7 +4,6 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
|
import org.dom4j.Document;
|
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
|
import org.dom4j.Element;
|
|
|
|
|
import org.dom4j.Node;
|
|
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
|
@ -24,15 +23,14 @@ public class TestOutSideExcel {
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static void compressAndUn(String sourceWordAddress) throws IOException {
|
|
|
|
|
File file = new File(sourceWordAddress);//取得word文件
|
|
|
|
|
String dir = "c:\\zipFile\\"; //取得要解压缩文件到的目录
|
|
|
|
|
public static void UnCompress(String wordPath, String workingPath) throws IOException {
|
|
|
|
|
File file = new File(wordPath);//取得word文件
|
|
|
|
|
FileInputStream inputStream = new FileInputStream(file);
|
|
|
|
|
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
|
|
|
|
|
ZipEntry entry;
|
|
|
|
|
byte ch[] = new byte[256];
|
|
|
|
|
byte[] ch = new byte[256];
|
|
|
|
|
while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
|
|
File zFile = new File(dir + entry.getName());
|
|
|
|
|
File zFile = new File(workingPath + entry.getName());
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
if (!zFile.exists()) {
|
|
|
|
|
zFile.mkdirs();
|
|
|
|
@ -57,37 +55,41 @@ public class TestOutSideExcel {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException {
|
|
|
|
|
String sourceDoc = "c:/西双版纳州人口变化及其对教育的影响20240420.docx";
|
|
|
|
|
int chartNumber = 1;
|
|
|
|
|
//List<List<String>> source = ExcelKit.getChartData(sourceDoc, chartNumber - 1, 0);
|
|
|
|
|
|
|
|
|
|
compressAndUn(sourceDoc);
|
|
|
|
|
//遍历目录 C:\zipFile\word\charts 下面有多少个chart?.xml的文件.下面我们来读取chart1.xml
|
|
|
|
|
|
|
|
|
|
String xml="C:\\zipFile\\word\\charts\\chart1.xml";
|
|
|
|
|
//1、将word文件解压缩
|
|
|
|
|
String workingPath = "C:\\zipFile";
|
|
|
|
|
UnCompress(sourceDoc, workingPath);
|
|
|
|
|
//2、我们需要第几个图表
|
|
|
|
|
int chartNumber = 1;
|
|
|
|
|
String xml = workingPath + "\\word\\charts\\chart" + chartNumber + ".xml";
|
|
|
|
|
if (!(new File(xml).exists())) {
|
|
|
|
|
System.out.println("没有找到第" + chartNumber + "个图表");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//3、开始读取
|
|
|
|
|
// 创建 SAXReader 对象,读取 XML 文件
|
|
|
|
|
SAXReader reader = new SAXReader();
|
|
|
|
|
Document document = reader.read(new File(xml));
|
|
|
|
|
SAXReader reader = new SAXReader();
|
|
|
|
|
Document document = reader.read(new File(xml));
|
|
|
|
|
// 获取根元素
|
|
|
|
|
Element root = document.getRootElement();
|
|
|
|
|
|
|
|
|
|
// 获取根元素
|
|
|
|
|
Element root = document.getRootElement();
|
|
|
|
|
System.out.println("根元素: " + root.getName());
|
|
|
|
|
///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache
|
|
|
|
|
List<Element> valueList=root.element("chart").element("plotArea")
|
|
|
|
|
.element("lineChart").element("ser").element("val")
|
|
|
|
|
//折线图
|
|
|
|
|
//将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")
|
|
|
|
|
.element("lineChart").element("ser").element("cat")
|
|
|
|
|
.element("numRef").element("numCache").elements("pt");
|
|
|
|
|
|
|
|
|
|
for (Element e : valueList) {
|
|
|
|
|
for (Element e : xList) {
|
|
|
|
|
System.out.println(e.element("v").getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Element> yearList=root.element("chart").element("plotArea")
|
|
|
|
|
.element("lineChart").element("ser").element("cat")
|
|
|
|
|
///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache
|
|
|
|
|
List<Element> yList = root.element("chart").element("plotArea")
|
|
|
|
|
.element("lineChart").element("ser").element("val")
|
|
|
|
|
.element("numRef").element("numCache").elements("pt");
|
|
|
|
|
|
|
|
|
|
for (Element e : yearList) {
|
|
|
|
|
for (Element e : yList) {
|
|
|
|
|
System.out.println(e.element("v").getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:cat/c:numRef/c:numCache/c:pt/c:v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|