main
黄海 8 months ago
parent 4a1c2a650b
commit 953fe19049

@ -61,36 +61,43 @@ public class C1 {
if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
System.out.println("正在处理" + cityName + "市州文件...");
//读取文件
System.out.println(file.getAbsolutePath());
//1号模板数据在图表5和28中
int firstChartNumber = 5, secondChartNumber = 28;
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.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
//遍历source1
for (List<String> r : source1) {
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "总入园数", "", r.get(4), "", cityName, "云南省")), dataStyle);
if (source1 != null) {
for (List<String> r : source1) {
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "总入园数", "", r.get(4), "", cityName, "云南省")), dataStyle);
}
}
//遍历source2
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;//跳过2022年这个年份重复了
List<String> r = source2.get(i);
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "总入园数", "", r.get(4), "", cityName, "云南省")), dataStyle);
if (source2 != null) {
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;//跳过2022年这个年份重复了
List<String> r = source2.get(i);
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "总入园数", "", r.get(4), "", cityName, "云南省")), dataStyle);
}
}
//2022年基数:遍历source2
for (int i = 0; i < source2.size(); i++) {
List<String> r = source2.get(i);
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "2022年基数(万人)", "", source2.getFirst().get(4),
"", cityName, "云南省")), dataStyle);
if (source2 != null) {
for (int i = 0; i < source2.size(); i++) {
List<String> r = source2.get(i);
Row outRow = outSheet.createRow(++rowIndex);
// 导出数据
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "2022年基数(万人)", "", source2.getFirst().get(4),
"", cityName, "云南省")), dataStyle);
}
}
//城区+镇区+乡村
@ -102,12 +109,14 @@ public class C1 {
for (int k = 0; k < areaList.size(); k++) {
String areaName = areaList.get(k);
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
Row outRow = outSheet.createRow(++rowIndex);
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "", areaName, "",
r.get(k + 1), cityName, "云南省")), dataStyle);
if (source1 != null) {
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
Row outRow = outSheet.createRow(++rowIndex);
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "", areaName, "",
r.get(k + 1), cityName, "云南省")), dataStyle);
}
}
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;

@ -94,8 +94,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, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber-1, 1);
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总在校生";
//遍历source1

@ -68,8 +68,8 @@ public class C5 {
//读取文件
//1号模板数据在图表11和34中
int firstChartNumber = 11, secondChartNumber = 34;
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.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总招生数";
//遍历source1

@ -68,8 +68,8 @@ public class C6 {
//读取文件
//1号模板数据在图表12和35中
int firstChartNumber = 12, secondChartNumber = 35;
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.getChartData(file.getAbsolutePath(), firstChartNumber, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber, 1);
String c2 = "总在校生数";
//遍历source1

@ -18,10 +18,7 @@ import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -551,80 +548,86 @@ public class ExcelKit {
//在每个已知的图表类型中查找,找到后跳出循环
//声明一个数组,图表的所有类型
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
String type = "";
for (String chartType : CHART_TYPES) {
if (root.element("chart").element("plotArea")
.element(chartType) != null) {
type = chartType;
break;
}
}
if (StrKit.isBlank(type)) {
System.out.println("没有找到图表类型,请扩充图表类型");
System.out.println(root.element("chart").element("plotArea"));
System.exit(-1);
}
List<Element> xList = root.element("chart").element("plotArea").element(type).element("ser").element("cat")
.element("numRef").element("numCache").elements("pt");
List<Object> listObject = 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");
//记录都有哪些有效数值和索引号
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 {
row.add(e.element("v").getText());
}
}
matrix.add(row);
}
//上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
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++) {
String x = matrix.get(row).get(col);
//如果x可以转为小数那么需要保留两位小数如果可以转换为整数那么直接返回整数如果是字符串就不变
try {
double d = Double.parseDouble(x);
if (d == (int) d) {
x = String.valueOf((int) d);
} else {
x = String.format("%.2f", d);
List<Element> xList = new ArrayList<>();
List<Element> children = root.element("chart").element("plotArea").elements();
for (Element child : children) {
//将CHART_TYPES数组转换为List
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);
if (CHART_TYPES_LIST.contains(child.getName())) {
transposed.clear();
// 处理barChart或lineChart节点
//System.out.println("找到图表类型:" + child.getName());
String type = child.getName();
xList = root.element("chart").element("plotArea").element(type).element("ser").element("cat")
.element("numRef").element("numCache").elements("pt");
List<Object> listObject = 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");
//记录都有哪些有效数值和索引号
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 {
row.add(e.element("v").getText());
}
}
} catch (Exception e) {
//do nothing
matrix.add(row);
}
//上面生成的数据格式需要行转列,横坐标是年份,纵坐标是数据
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);
//如果x可以转为小数那么需要保留两位小数如果可以转换为整数那么直接返回整数如果是字符串就不变
try {
double d = Double.parseDouble(x);
if (d == (int) d) {
x = String.valueOf((int) d);
} else {
x = String.format("%.2f", d);
}
} catch (Exception e) {
//do nothing
}
column.add(x);
}
// 将当前列添加到结果列表中
transposed.add(column);
}
column.add(x);
}
// 将当前列添加到结果列表中
transposed.add(column);
}
// printTable(transposed);
// 在每一行的第一列插入年份
for (int i = 0; i < transposed.size(); i++) {
List<String> column = transposed.get(i);

File diff suppressed because it is too large Load Diff

@ -14,9 +14,9 @@ public class TestOutSideExcel {
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, ParserConfigurationException, SAXException, XPathExpressionException, DocumentException {
String sourceDoc = "c:/西双版纳州人口变化及其对教育的影响20240420.docx";
String sourceDoc = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510\\临沧市人口变化及其对教育的影响20240419.docx";
//需要第几个图表
for (int chartNumber = 1; chartNumber <= 37; chartNumber++) {
for (int chartNumber = 5; chartNumber <= 5; chartNumber++) {
System.out.println("正在处理第" + chartNumber + "个图表的信息~");
//读取图表
List<List<String>> list = ExcelKit.readChart(sourceDoc, chartNumber);

Loading…
Cancel
Save