|
|
|
@ -0,0 +1,100 @@
|
|
|
|
|
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 A10 {
|
|
|
|
|
|
|
|
|
|
//示例Excel
|
|
|
|
|
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【10】新-概览\\新-概览.xlsx";
|
|
|
|
|
|
|
|
|
|
//源文件
|
|
|
|
|
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\2023年数据更新表\\16州市数据更新表汇总20241021";
|
|
|
|
|
|
|
|
|
|
//哪些是处理不了的,就不处理了~
|
|
|
|
|
static String[] excludeCityList = {"~$", "磨憨-磨丁", "经开区", "阳宗海"};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//第二个表格的表头信息
|
|
|
|
|
static String tableKeyword = "2023年州市、县区人口及教育数据表";
|
|
|
|
|
//第二个表格的表头有几行
|
|
|
|
|
static int tableHeadRows = 4;
|
|
|
|
|
//第二个表格的最后一列的列名
|
|
|
|
|
static String tableLetterIndex = "BA";
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InvalidFormatException {
|
|
|
|
|
//初始化数据库连接
|
|
|
|
|
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(".xlsx") && !file.getName().endsWith(".xls"))
|
|
|
|
|
continue;
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
for (String s : excludeCityList) {
|
|
|
|
|
if (file.getName().contains(s)) {
|
|
|
|
|
flag = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (flag) continue;
|
|
|
|
|
//县区名称
|
|
|
|
|
String cityName = ru.getCityOrAreaName(file.getName());
|
|
|
|
|
|
|
|
|
|
if (StrKit.isBlank(cityName)) {
|
|
|
|
|
System.out.println("发现异常数据,请人工处理:" + file.getName());
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//县区名称
|
|
|
|
|
System.out.println("正在进行" + cityName + "的数据填充~");
|
|
|
|
|
String sourceExcel = file.getAbsolutePath();
|
|
|
|
|
List<List<String>> dataList = ExcelKit.readSecondTable(sourceExcel, 0, tableKeyword,
|
|
|
|
|
tableHeadRows, tableLetterIndex);
|
|
|
|
|
|
|
|
|
|
ExcelKit.printTable(dataList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//保存文件
|
|
|
|
|
ExcelKit.saveExcel(excelPath, outWorkbook);
|
|
|
|
|
System.out.println("县区所有文件处理完成!");
|
|
|
|
|
}
|
|
|
|
|
}
|