|
|
|
@ -16,7 +16,9 @@ import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
|
|
|
|
|
@ -66,11 +68,11 @@ public class TestOutSideExcel {
|
|
|
|
|
* @param chartNumber
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> readChar(String workingPath, int chartNumber) throws DocumentException {
|
|
|
|
|
List<List<String>> res = new ArrayList<>();
|
|
|
|
|
List<List<String>> matrix = new ArrayList<>();
|
|
|
|
|
String xml = workingPath + "\\word\\charts\\chart" + chartNumber + ".xml";
|
|
|
|
|
if (!(new File(xml).exists())) {
|
|
|
|
|
System.out.println("没有找到第" + chartNumber + "个图表");
|
|
|
|
|
return res;
|
|
|
|
|
return matrix ;
|
|
|
|
|
}
|
|
|
|
|
//3、开始读取
|
|
|
|
|
// 创建 SAXReader 对象,读取 XML 文件
|
|
|
|
@ -105,35 +107,66 @@ public class TestOutSideExcel {
|
|
|
|
|
.element("numRef").element("numCache").elements("pt");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<List<Element>> allValueList = new ArrayList<>();
|
|
|
|
|
List<Object> allValueList = new ArrayList<>();
|
|
|
|
|
///c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache
|
|
|
|
|
for (Element ser : root.element("chart").element("plotArea")
|
|
|
|
|
.element(type).elements("ser")) {
|
|
|
|
|
List<Element> yList = ser.element("val")
|
|
|
|
|
.element("numRef").element("numCache").elements("pt");
|
|
|
|
|
allValueList.add(yList);
|
|
|
|
|
List<Element> yList = ser.element("val").element("numRef").element("numCache").elements("pt");
|
|
|
|
|
|
|
|
|
|
//记录都有哪些有效数值和索引号
|
|
|
|
|
Map<Integer, Element> map = new HashMap<>();
|
|
|
|
|
for (Element e : yList) {
|
|
|
|
|
map.put(Integer.parseInt(e.attribute("idx").getValue()), e);
|
|
|
|
|
}
|
|
|
|
|
List<Object> lo = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < xList.size(); i++) {
|
|
|
|
|
if (map.containsKey(i)) {
|
|
|
|
|
lo.add(map.get(i));
|
|
|
|
|
} else {
|
|
|
|
|
lo.add(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
allValueList.add(lo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < xList.size(); i++) {
|
|
|
|
|
for (int i = 0; i < allValueList.size(); i++) {
|
|
|
|
|
List<String> row = new ArrayList<>();
|
|
|
|
|
row.add(xList.get(i).element("v").getText());
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < allValueList.size(); j++) {
|
|
|
|
|
List<Element> yList = allValueList.get(j);
|
|
|
|
|
if (yList == null || i + 1 > yList.size() || yList.get(i) == null) {
|
|
|
|
|
List<Element> lo = (List<Element>) allValueList.get(i);
|
|
|
|
|
for (Element e : lo) {
|
|
|
|
|
if (e == null) {
|
|
|
|
|
row.add(null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Element e = yList.get(i).element("v");
|
|
|
|
|
if (e != null) {
|
|
|
|
|
row.add(e.getText());
|
|
|
|
|
} else {
|
|
|
|
|
row.add(null);
|
|
|
|
|
row.add(e.element("v").getText());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.add(row);
|
|
|
|
|
matrix.add(row);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
//上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
|
|
|
|
|
// 计算行数和列数
|
|
|
|
|
int rowCount = matrix.size();
|
|
|
|
|
int colCount = matrix.getFirst().size();
|
|
|
|
|
|
|
|
|
|
// 创建一个一维列表,用于存储转换后的列
|
|
|
|
|
List<List<String>> transposed = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 遍历每一列
|
|
|
|
|
for (int col = 0; col < colCount; col++) {
|
|
|
|
|
// 创建一个新的内部列表,用于存储当前列的所有行
|
|
|
|
|
List<String> column = new ArrayList<>();
|
|
|
|
|
// 遍历每一行,将当前列的值添加到新的内部列表中
|
|
|
|
|
for (int row = 0; row < rowCount; row++) {
|
|
|
|
|
column.add(matrix.get(row).get(col));
|
|
|
|
|
}
|
|
|
|
|
// 将当前列添加到结果列表中
|
|
|
|
|
transposed.add(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在每一行的第一列插入序号
|
|
|
|
|
for (int i = 0; i < transposed.size(); i++) {
|
|
|
|
|
List<String> column = transposed.get(i);
|
|
|
|
|
column.addFirst(xList.get(i).element("v").getText()); // 在每行的开始插入序号
|
|
|
|
|
}
|
|
|
|
|
return transposed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException {
|
|
|
|
@ -143,7 +176,6 @@ public class TestOutSideExcel {
|
|
|
|
|
String workingPath = "C:\\zipFile";
|
|
|
|
|
UnCompress(sourceDoc, workingPath);
|
|
|
|
|
//2、我们需要第几个图表
|
|
|
|
|
|
|
|
|
|
for (int chartNumber = 4; chartNumber <= 4; chartNumber++) {
|
|
|
|
|
System.out.println("正在处理第" + chartNumber + "个图表的信息~");
|
|
|
|
|
//读取图表
|
|
|
|
|