main
黄海 8 months ago
parent 70bfe97327
commit 85a1bbfbd2

@ -13,11 +13,8 @@ import org.dom4j.DocumentException;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
public class A12 {
//示例Excel
@ -88,7 +85,7 @@ public class A12 {
//县区名称
//System.out.println(file.getAbsolutePath());
int chartNumber = 36;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), chartNumber, 0);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), chartNumber, 0);
//需要处理下人与万人的单位兼容
// for (int i = 0; i < source1.size(); i++) {

@ -6,7 +6,6 @@ import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import com.jfinal.kit.StrKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -14,7 +13,6 @@ import org.dom4j.DocumentException;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class A8 {
@ -94,7 +92,7 @@ public class A8 {
//System.out.println("正在进行" + cityName + "-" + areaName + "的数据填充~");
int firstChartNumber = 1;
List<List<String>> source1 =
ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
}

@ -92,11 +92,11 @@ public class A9 {
//System.out.println("正在进行" + cityName + "-" + areaName + "的数据填充~");
int chartNumber = 2;
List<List<String>> source2 =
ExcelKit.getChartData(file.getAbsolutePath(), chartNumber - 1, 6);
ExcelKit.readChart(file.getAbsolutePath(), chartNumber - 1, 6);
chartNumber = 3;
List<List<String>> source3 =
ExcelKit.getChartData(file.getAbsolutePath(), chartNumber - 1, 6);
ExcelKit.readChart(file.getAbsolutePath(), chartNumber - 1, 6);
}
}

@ -65,8 +65,8 @@ public class C1 {
//1号模板数据在图表5和28中
int firstChartNumber = 5, secondChartNumber = 28;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber, 1);
//遍历source1
if (source1 != null) {

@ -6,19 +6,14 @@ import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import com.jfinal.kit.StrKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -70,9 +65,9 @@ public class C10 {
//数据在图表2,图3
int firstChartNumber = 2, secondChartNumber = 3;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
List<List<String>> source3 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 2);
List<List<String>> source3 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber - 1, 2);
;//从2023年开始
//遍历source1

@ -5,19 +5,14 @@ import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -71,7 +66,7 @@ public class C11 {
int firstChartNumber = 36, secondChartNumber = 37;
//占地面积37
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 1);
//年份,学前,小学,初中,高中
// 0 1 2 3 4
//所有相关2022基数的列都使用2022的数据
@ -84,7 +79,7 @@ public class C11 {
int gxjzgJS = (int) (Double.parseDouble(firRow.get(4)) * 10000);//高中教职工2022基数
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber - 1, 1);
//年份,学前,小学,初中,高中
// 0 1 2 3 4
List<String> secRow = source2.getFirst();

@ -6,19 +6,14 @@ import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import com.jfinal.kit.StrKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -70,8 +65,8 @@ public class C2 {
//1号模板数据在图表6和29中
int firstChartNumber = 6, secondChartNumber = 29;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber - 1, 1);
String c2 = "总在园数";
//遍历source1

@ -90,8 +90,8 @@ public class C3 {
int firstChartNumber = listNode.get(q).getFirst(), secondChartNumber = listNode.get(q).getSecond();
String stageName = listNode.get(q).getStageName();
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber - 1, 1);
String c2 = "总招生数";
//遍历source1

@ -7,19 +7,14 @@ import com.dsideal.base.Tools.Util.ReadDocxUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -94,8 +89,8 @@ public class C4 {
int firstChartNumber = listNode.get(q).getFirst(), secondChartNumber = listNode.get(q).getSecond();
String stageName=listNode.get(q).getStageName();
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总在校生";
//遍历source1

@ -5,19 +5,14 @@ import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -68,8 +63,8 @@ public class C5 {
//读取文件
//1号模板数据在图表11和34中
int firstChartNumber = 11, secondChartNumber = 34;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总招生数";
//遍历source1

@ -5,19 +5,14 @@ import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
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.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -68,8 +63,8 @@ public class C6 {
//读取文件
//1号模板数据在图表12和35中
int firstChartNumber = 12, secondChartNumber = 35;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.readChart(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总在校生数";
//遍历source1

@ -72,7 +72,7 @@ public class C9 {
//chartNumber:第几个图表
//skipRowCount:跳过的行数,2017年开始,第一行是表头第二行开始是2022所以填写了跳过6行
//expectLimit:期望的数据行数,首先用POI进行解析如果获取的行数大于预期行数就是正确的否则就需要二次调用python进行读取
List<List<String>> source = ExcelKit.getChartData(file.getAbsolutePath(), chartNumber - 1, 6);
List<List<String>> source = ExcelKit.readChart(file.getAbsolutePath(), chartNumber - 1, 6);
System.out.println("当前城市数据条目数量=" + source.size());

@ -365,7 +365,7 @@ public class ExcelKit {
return data;
}
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, InvalidFormatException, DocumentException, InterruptedException {
public static List<List<String>> readChart(String docPath, int chartNumber, int skipRowCount) throws IOException, InvalidFormatException, DocumentException, InterruptedException {
return getChartDataByXml(docPath, chartNumber, skipRowCount);
// InputStream is = new FileInputStream(docPath);
@ -548,129 +548,125 @@ public class ExcelKit {
UnCompress(sourceDoc);
List<List<String>> matrix = new ArrayList<>();
String xml = DocxUnzipDirectory + "word\\charts\\chart" + chartNumber + ".xml";
if (!(new File(xml).exists())) {
System.out.println(sourceDoc + " 没有找到第" + chartNumber + "个图表");
return matrix;
}
//3、开始读取
// 创建 SAXReader 对象,读取 XML 文件
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
//声明一个数组,图表的所有类型
return readChart(xml);
}
/**
*
*
* @param v
* @return
*/
public static String doubleWith2f(String v) {
// 保留两位小数
try {
double d = Double.parseDouble(v);
if (d == (int) d) {
v = String.valueOf((int) d);
} else {
v = String.format("%.2f", d);
}
} catch (Exception err) {
//do nothing
}
return v;
}
/**
* XML
*
* @param xmlPath
* @return
* @throws DocumentException
*/
public static List<List<String>> readChart(String xmlPath) throws DocumentException {
//图表类型
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
// 创建一个一维列表,用于存储转换后的列
List<List<String>> transposed = new ArrayList<>();
SAXReader reader = new SAXReader(); // 创建 SAXReader 对象,读取 XML 文件
Document document = reader.read(new File(xmlPath));
Element root = document.getRootElement();// 获取根元素
//行数据库不管是X轴的年份还是Y轴的数值都按行保存到一个二维列表中
List<List<String>> tList = new ArrayList<>();
List<Object> listObject = new ArrayList<>();
List<Element> xList = new ArrayList<>();//横坐标,一般是年份
List<Element> children = root.element("chart").element("plotArea").elements();//工作区
boolean hadReadXList = false;//是不是已经读取过横坐标也就是年份的数据了
for (Element child : children) {
//将CHART_TYPES数组转换为List
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);//将CHART_TYPES数组转换为List
if (CHART_TYPES_LIST.contains(child.getName())) {//如果当前遍历到的子元素是折线图或者柱状图
System.out.println("找到图表类型:" + child.getName());
//System.out.println("找到图表类型:" + child.getName());
String type = child.getName();
//ce:chart element
Element ce = root.element("chart").element("plotArea").element(type);
System.out.println("Count=" + ce.elements("ser").size());
Set<Element> set=new HashSet<>();
for (int m = 0; m < ce.elements("ser").size(); m++) {
Element ser = ce.elements("ser").get(m);
//System.out.println(ser);
for (int i = 0; i < ce.elements("ser").size(); i++) {
Element ser = ce.elements("ser").get(i);
//cat 标签
Element cat = ser.element("cat");
Element numRef = cat.element("numRef");
Element f = numRef.element("f");
System.out.println(f);
System.out.println(f.getText());
//数据
Element numCache = numRef.element("numCache");
xList = numCache.elements("pt");
System.out.println(xList);
//多个数据组xList(横坐标年份数据)一定是一样的,并且它是最全的,只读取一次即可
if (!hadReadXList) {
List<Element> xList = numCache.elements("pt");
List<String> yearList = new ArrayList<>();
for (Element pt : xList) {
String v = pt.element("v").getText();
yearList.add(v);
}
//添加到横向的表中
tList.add(yearList);
hadReadXList = true;//标识读取过了
}
//======================================================
//val标签
Element val = ser.element("val");
numRef = val.element("numRef");
numCache = numRef.element("numCache");
List<Element> yList = numCache.elements("pt");
//不是每个索引号都一定有数据的所以先将存在的索引号保存到HashMap中,后面再遍历每个年份的索引号时有的就读取数值没有的就写成null
Map<String, String> _map = new HashMap<>();
for (Element pt : yList) {
String idx = pt.attribute("idx").getValue();
String v = pt.element("v").getText();
//保留两位小数的字符串
v = doubleWith2f(v);
//保存到map中一会再遍历时就知道哪个有哪个没有没有的需要填充null
_map.put(idx, v);
}
//枚举一遍全的xList
List<String> vList = new ArrayList<>();
//首行就是年份行它的size()就是标准宽度
for (int j = 0; j < tList.getFirst().size(); j++) {
vList.add(_map.getOrDefault(String.valueOf(j), null));
}
tList.add(vList);
}
}
}
///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");
//
// //记录都有哪些有效数值和索引号
// Map<Integer, Element> map = new HashMap<>();
// for (Element e : yList) {
// map.put(Integer.parseInt(e.attribute("idx").getValue()), e);
// }
// List<Object> list = new ArrayList<>();
// for (int i = 0; i < xList.size(); i++) {
// list.add(map.getOrDefault(i, null));
// }
// listObject.add(list);
// }
//
//
// //数据是按行读取的
// for (int i = 0; i < listObject.size(); i++) {
// List<String> row = new ArrayList<>();
// List<Element> lo = (List<Element>) listObject.get(i);
// for (Element e : lo) {
// if (e == null) {
// row.add(null);
// } else {
// //如果x可以转为小数那么需要保留两位小数如果可以转换为整数那么直接返回整数如果是字符串就不变
// String x = null;
// try {
// x = e.element("v").getText();
// double d = Double.parseDouble(x);
// if (d == (int) d) {
// x = String.valueOf((int) d);
// } else {
// x = String.format("%.2f", d);
// }
// } catch (Exception err) {
// //do nothing
// }
// row.add(x);
// }
// }
// matrix.add(row);
// }
//
// printTable(matrix);
//
//
// //上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
// int rowCount = matrix.size();// 计算行数和列数
// int colCount = matrix.getFirst().size();
//
// //System.out.println("行数:" + rowCount + " " + "列数:" + colCount);
//
// // 遍历每一列
// for (int col = 0; col < colCount; col++) {
// // 创建一个新的内部列表,用于存储当前列的所有行
// List<String> column = new ArrayList<>();
// // 遍历每一行,将当前列的值添加到新的内部列表中
// for (int row = 0; row < rowCount; row++) {
// String x = matrix.get(row).get(col);
//
// column.add(x);
// }
// // 将当前列添加到结果列表中
// transposed.add(column);
// }
//实现行列转置
// 创建一个一维列表,用于存储转换后的列
List<List<String>> res = new ArrayList<>();
//上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
int rowCount = tList.size();// 计算行数和列数
int colCount = tList.getFirst().size();
// 遍历每一列
for (int col = 0; col < colCount; col++) {
// 创建一个新的内部列表,用于存储当前列的所有行
List<String> column = new ArrayList<>();
// 遍历每一行,将当前列的值添加到新的内部列表中
for (int row = 0; row < rowCount; row++) {
String x = tList.get(row).get(col);
column.add(x);
}
// 将当前列添加到结果列表中
res.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;
return res;
}
}

@ -1,20 +1,12 @@
package com.dsideal.base.Tools.FillData.Test;
import cn.hutool.core.io.FileUtil;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.DocumentException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class C11 {
@ -30,7 +22,7 @@ public class C11 {
int firstChartNumber = 36, secondChartNumber = 37;
//占地面积37
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source1 = ExcelKit.readChart(file.getAbsolutePath(), firstChartNumber - 1, 1);
ExcelKit.printTable(source1);
}
}

@ -2,12 +2,6 @@ package com.dsideal.base.Tools.FillData.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;
import org.dom4j.DocumentException;
import java.io.*;

@ -1,137 +1,27 @@
package com.dsideal.base.Tools.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class TestXml {
/**
*
*
* @param v
* @return
*/
public static String doubleWith2f(String v) {
// 保留两位小数
try {
double d = Double.parseDouble(v);
if (d == (int) d) {
v = String.valueOf((int) d);
} else {
v = String.format("%.2f", d);
}
} catch (Exception err) {
//do nothing
}
return v;
}
/**
* XML
*
* @param xmlPath
* @return
* @throws DocumentException
*/
public static List<List<String>> getChartData(String xmlPath) throws DocumentException {
//图表类型
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
SAXReader reader = new SAXReader(); // 创建 SAXReader 对象,读取 XML 文件
Document document = reader.read(new File(xmlPath));
Element root = document.getRootElement();// 获取根元素
//行数据库不管是X轴的年份还是Y轴的数值都按行保存到一个二维列表中
List<List<String>> tList = new ArrayList<>();
List<Element> children = root.element("chart").element("plotArea").elements();//工作区
boolean hadReadXList = false;//是不是已经读取过横坐标也就是年份的数据了
for (Element child : children) {
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);//将CHART_TYPES数组转换为List
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);
for (int i = 0; i < ce.elements("ser").size(); i++) {
Element ser = ce.elements("ser").get(i);
//cat 标签
Element cat = ser.element("cat");
Element numRef = cat.element("numRef");
//数据
Element numCache = numRef.element("numCache");
//多个数据组xList(横坐标年份数据)一定是一样的,并且它是最全的,只读取一次即可
if (!hadReadXList) {
List<Element> xList = numCache.elements("pt");
List<String> yearList = new ArrayList<>();
for (Element pt : xList) {
String v = pt.element("v").getText();
yearList.add(v);
}
//添加到横向的表中
tList.add(yearList);
hadReadXList = true;//标识读取过了
}
//======================================================
//val标签
Element val = ser.element("val");
numRef = val.element("numRef");
numCache = numRef.element("numCache");
List<Element> yList = numCache.elements("pt");
//不是每个索引号都一定有数据的所以先将存在的索引号保存到HashMap中,后面再遍历每个年份的索引号时有的就读取数值没有的就写成null
Map<String, String> _map = new HashMap<>();
for (Element pt : yList) {
String idx = pt.attribute("idx").getValue();
String v = pt.element("v").getText();
//保留两位小数的字符串
v = doubleWith2f(v);
//保存到map中一会再遍历时就知道哪个有哪个没有没有的需要填充null
_map.put(idx, v);
}
//枚举一遍全的xList
List<String> vList = new ArrayList<>();
//首行就是年份行它的size()就是标准宽度
for (int j = 0; j < tList.getFirst().size(); j++) {
vList.add(_map.getOrDefault(String.valueOf(j), null));
}
tList.add(vList);
}
}
}
public static void main(String[] args) throws DocumentException, IOException {
//String sourceDoc="c:/西双版纳州人口变化及其对教育的影响20240420.docx";
//将word文件解压缩
//ExcelKit.UnCompress(sourceDoc);
//实现行列转置
// 创建一个一维列表,用于存储转换后的列
List<List<String>> res = new ArrayList<>();
//上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
int rowCount = tList.size();// 计算行数和列数
int colCount = tList.getFirst().size();
// 遍历每一列
for (int col = 0; col < colCount; col++) {
// 创建一个新的内部列表,用于存储当前列的所有行
List<String> column = new ArrayList<>();
// 遍历每一行,将当前列的值添加到新的内部列表中
for (int row = 0; row < rowCount; row++) {
String x = tList.get(row).get(col);
column.add(x);
}
// 将当前列添加到结果列表中
res.add(column);
for (int chartNumber = 1; chartNumber <= 37; chartNumber++) {
String xml = ExcelKit.DocxUnzipDirectory + "word\\charts\\chart" + chartNumber + ".xml";
List<List<String>> res = ExcelKit.readChart(xml);
//输出转置后的数据
System.out.println("正在输出第" + chartNumber + "个图表的数据");
ExcelKit.printTable(res);
}
return res;
}
public static void main(String[] args) throws DocumentException {
String xml = "D:\\dsWork\\YunNanDsBase\\src\\main\\java\\com\\dsideal\\base\\Tools\\Test\\Sample.xml";
List<List<String>> res = getChartData(xml);
//输出转置后的数据
ExcelKit.printTable(res);
}
}
Loading…
Cancel
Save