|
|
|
@ -0,0 +1,169 @@
|
|
|
|
|
package com.dsideal.base.Tools.FillData.City;
|
|
|
|
|
|
|
|
|
|
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 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 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;
|
|
|
|
|
|
|
|
|
|
public class C4 {
|
|
|
|
|
//开始读取市州word文档
|
|
|
|
|
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
|
|
|
|
|
|
|
|
|
|
//示例Excel
|
|
|
|
|
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【4】义务教育在校生总量变化及预测\\义务教育在校生总量变化及预测.xlsx";
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
@Setter
|
|
|
|
|
public static class Node {
|
|
|
|
|
int first;
|
|
|
|
|
int second;
|
|
|
|
|
String stageName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InvalidFormatException {
|
|
|
|
|
//初始化数据库连接
|
|
|
|
|
LocalMysqlConnectUtil.Init();
|
|
|
|
|
//实例化
|
|
|
|
|
ReadDocxUtil ru = new ReadDocxUtil();
|
|
|
|
|
|
|
|
|
|
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
|
|
|
|
|
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
|
|
|
|
|
ExcelKit.delExcel(excelPath);
|
|
|
|
|
|
|
|
|
|
//结果Excel
|
|
|
|
|
XSSFWorkbook outWorkbook = new XSSFWorkbook();
|
|
|
|
|
//结果Sheet
|
|
|
|
|
XSSFSheet outSheet = ExcelKit.CreateSheet(outWorkbook);
|
|
|
|
|
//样式
|
|
|
|
|
XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook);
|
|
|
|
|
XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook);
|
|
|
|
|
|
|
|
|
|
//拷贝文件头
|
|
|
|
|
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
|
|
|
|
|
|
|
|
|
|
//找到parentPath下一级目录中所有文件
|
|
|
|
|
List<File> files = FileUtil.loopFiles(parentPath, file -> true);
|
|
|
|
|
int rowIndex = 0;
|
|
|
|
|
//处理这个目录
|
|
|
|
|
if (files != null) {
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
//判断file是不是目录,是目录的需要跳过
|
|
|
|
|
if (file.isDirectory()) continue;
|
|
|
|
|
//城市名称
|
|
|
|
|
String cityName = ru.getCityOrAreaName(file.getName());
|
|
|
|
|
String fileName = file.getName();
|
|
|
|
|
|
|
|
|
|
//判断是否为docx文件
|
|
|
|
|
if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
|
|
|
|
|
System.out.println("正在处理" + cityName + "市州文件...");
|
|
|
|
|
//读取文件
|
|
|
|
|
String inputUrl = file.getAbsolutePath();
|
|
|
|
|
InputStream is = new FileInputStream(inputUrl);
|
|
|
|
|
ZipSecureFile.setMinInflateRatio(-1.0d);
|
|
|
|
|
XWPFDocument doc = new XWPFDocument(is);
|
|
|
|
|
//排序后的图表
|
|
|
|
|
List<XWPFChart> charts = ExcelKit.getSortListForXWPFChart(doc.getCharts());
|
|
|
|
|
|
|
|
|
|
List<Node> listNode = new ArrayList<>();
|
|
|
|
|
Node x = new Node();
|
|
|
|
|
x.setFirst(8);
|
|
|
|
|
x.setSecond(31);
|
|
|
|
|
x.setStageName("小学");
|
|
|
|
|
listNode.add(x);
|
|
|
|
|
|
|
|
|
|
x = new Node();
|
|
|
|
|
x.setFirst(10);
|
|
|
|
|
x.setSecond(33);
|
|
|
|
|
x.setStageName("初中");
|
|
|
|
|
listNode.add(x);
|
|
|
|
|
|
|
|
|
|
//1号模板,数据在图表(8,31),(10,33)
|
|
|
|
|
for (int q = 0; q < listNode.size(); q++) {
|
|
|
|
|
int firstChartNumber = listNode.get(q).getFirst(), secondChartNumber = listNode.get(q).getSecond();
|
|
|
|
|
String stageName=listNode.get(q).getStageName();
|
|
|
|
|
|
|
|
|
|
XSSFWorkbook workbook = charts.get(firstChartNumber - 1).getWorkbook();
|
|
|
|
|
List<List<String>> source1 = ExcelKit.readSheet(workbook);
|
|
|
|
|
XSSFWorkbook workbook2 = charts.get(secondChartNumber - 1).getWorkbook();
|
|
|
|
|
List<List<String>> source2 = ExcelKit.readSheet(workbook2);
|
|
|
|
|
|
|
|
|
|
String c2 = "总在校生";
|
|
|
|
|
//遍历source1
|
|
|
|
|
for (List<String> r : source1) {
|
|
|
|
|
Row outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
// 导出数据
|
|
|
|
|
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
|
|
|
|
|
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "", c2, stageName, "", 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(), "", c2, stageName, "", 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年基数", stageName, "", source2.getFirst().get(4),
|
|
|
|
|
cityName, "云南省")), dataStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//城区+镇区+乡村
|
|
|
|
|
List<String> areaList = new ArrayList<>();
|
|
|
|
|
areaList.add("城区");
|
|
|
|
|
areaList.add("镇区");
|
|
|
|
|
areaList.add("乡村");
|
|
|
|
|
|
|
|
|
|
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, "",
|
|
|
|
|
stageName, "", r.get(k + 1), cityName, "云南省")), dataStyle);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < source2.size(); i++) {
|
|
|
|
|
if (i == 0) continue;
|
|
|
|
|
List<String> r = source2.get(i);
|
|
|
|
|
Row outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
|
|
|
|
|
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), areaName, "",
|
|
|
|
|
stageName, "", r.get(k + 1), cityName, "云南省")), dataStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//保存文件
|
|
|
|
|
ExcelKit.saveExcel(excelPath, outWorkbook);
|
|
|
|
|
System.out.println("市州所有文件处理完成!");
|
|
|
|
|
}
|
|
|
|
|
}
|