main
黄海 8 months ago
parent 760d3810e7
commit 933b3501a8

@ -0,0 +1,104 @@
package com.dsideal.base.Tools.FillData.Area;
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 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;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class A8 {
//示例Excel
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【8】总人口变化及预测-双\\总人口变化及预测-双-(万人).xlsx";
//源文件
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告";
//哪些是处理不了的,就不处理了~
static String[] excludeCityList = {"~$", "磨憨-磨丁", "经开区", "阳宗海"};
//有好多EXCEL啥样的有用必须带有关键字字样的才有用!
static String fileNameKey = "人口变化及其对教育的影响";
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException {
//初始化数据库连接
LocalMysqlConnectUtil.Init();
//实例化
ReadDocxUtil ru = new ReadDocxUtil();
//结果Excel
XSSFWorkbook outWorkbook = new XSSFWorkbook();
//结果Sheet
XSSFSheet outSheet = ExcelKit.CreateSheet(outWorkbook);
//样式
XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook);
XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook);
//如果样例文件是xls格式则转化为xlsx格式
sampleExcelPath = ExcelKit.ConvertXlsToXlsx(sampleExcelPath);
//拷贝文件头
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
ExcelKit.delExcel(excelPath);
//找到parentPath下一级目录中所有文件
List<File> files = FileUtil.loopFiles(parentPath, file -> true);
int rowIndex = 0;
//处理这个目录
if (files != null) {
for (File file : files) {
//判断file是不是目录是目录的需要跳过
if (file.isDirectory()) continue;
if (!file.getName().endsWith(".docx") || file.getName().startsWith("~"))
continue;
boolean flag = false;
for (String s : excludeCityList) {
if (file.getName().contains(s)) {
flag = true;
break;
}
}
if (flag) continue;
//只关心发展规模数据的表格
if (!file.getName().contains(fileNameKey)) continue;
//县区名称
String areaName = ru.getCityOrAreaName(file.getName());
//市州名称
//此文件的路径中最后一层的目录名称,应该是带着市州名称 ,把最后一层子目录的名称提出
String parentPathName = file.getParentFile().getName();
String cityName = ru.getCityOrAreaName(parentPathName);
if (StrKit.isBlank(cityName) || cityName.equals(areaName)) {
parentPathName = file.getParentFile().getParentFile().getName();
cityName = ru.getCityOrAreaName(parentPathName);
}
if (StrKit.isBlank(cityName) || StrKit.isBlank(areaName)) {
System.out.println("发现异常数据,请人工处理:" + file.getName());
System.exit(0);
}
int firstChartNumber = 1;
List<List<String>> source1 =
ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
//县区名称
System.out.println("正在进行" + cityName + "-" + areaName + "的数据填充~");
}
}
//保存文件
ExcelKit.saveExcel(excelPath, outWorkbook);
System.out.println("县区所有文件处理完成!");
}
}

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

@ -69,9 +69,9 @@ public class C10 {
//数据在图表2,图3
int firstChartNumber = 2, secondChartNumber = 3;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 6, 10);//从2017年开始
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 6);//从2017年开始
List<List<String>> source3 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 2, 10);
List<List<String>> source3 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 2);
;//从2023年开始
//遍历source1

@ -70,7 +70,7 @@ public class C11 {
int firstChartNumber = 36, secondChartNumber = 37;
//占地面积37
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1, 10);
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
//年份,学前,小学,初中,高中
// 0 1 2 3 4
//所有相关2022基数的列都使用2022的数据
@ -83,7 +83,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, 10);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1);
//年份,学前,小学,初中,高中
// 0 1 2 3 4
List<String> secRow = source2.getFirst();

@ -69,8 +69,8 @@ public class C2 {
//1号模板数据在图表6和29中
int firstChartNumber = 6, secondChartNumber = 29;
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1, 10);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1, 10);
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
List<List<String>> source2 = ExcelKit.getChartData(file.getAbsolutePath(), secondChartNumber - 1, 1);
String c2 = "总在园数";
//遍历source1

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

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

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

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

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

@ -340,6 +340,7 @@ public class ExcelKit {
}
return --rlt;
}
/**
*
*
@ -350,7 +351,7 @@ public class ExcelKit {
* @throws IOException
* @throws InvalidFormatException
*/
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount, int expectLimit) throws IOException, InvalidFormatException, InterruptedException {
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, InvalidFormatException, InterruptedException {
InputStream is = new FileInputStream(docPath);
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
@ -359,14 +360,9 @@ public class ExcelKit {
XSSFWorkbook workbook = charts.get(chartNumber).getWorkbook();
List<List<String>> data = ExcelKit.readSheet(workbook, skipRowCount);
is.close();
//如果达到目标预期的数量就直接返回poi获取的数据列表
int totalRow = data.size() + skipRowCount;
if (totalRow < expectLimit) {
System.out.println("poi获取的数据列表" + data.size());
printTable(data);
System.out.println("怎么能小于预期目标:" + expectLimit + "呢,请人为检查!文件路径:" + docPath + "\n第" + chartNumber + "个图表。");
System.exit(0);
if (data.isEmpty()) {
System.out.println("第" + (chartNumber + 1) + "个图表Sheet个数="+workbook.getNumberOfSheets()+",Sheet名称="+workbook.getSheetAt(0).getSheetName()+",数据为空,请检查表格是否为空");
System.exit(-1);
}
return data;
}
@ -467,6 +463,7 @@ public class ExcelKit {
/**
*
*
* @param dataList
*/
public static void printTable(List<List<String>> dataList) {
@ -480,6 +477,7 @@ public class ExcelKit {
/**
* Sheet
*
* @param sheet
*/
public static void printSheet(XSSFSheet sheet) {

@ -29,7 +29,7 @@ public class C11 {
int firstChartNumber = 36, secondChartNumber = 37;
//占地面积37
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1, 10);
List<List<String>> source1 = ExcelKit.getChartData(file.getAbsolutePath(), firstChartNumber - 1, 1);
ExcelKit.printTable(source1);
}
}

Loading…
Cancel
Save