|
|
|
@ -0,0 +1,101 @@
|
|
|
|
|
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 A1 {
|
|
|
|
|
|
|
|
|
|
//示例Excel
|
|
|
|
|
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【8】人口总体情况\\人口总体情况.xlsx";
|
|
|
|
|
|
|
|
|
|
//源文件
|
|
|
|
|
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\2023年数据更新表\\16州市数据更新表汇总20241021";
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
//城市名称
|
|
|
|
|
String cityName = ru.getCityOrAreaName(file.getName());
|
|
|
|
|
if (StrKit.isBlank(cityName)) {
|
|
|
|
|
System.out.println("发现异常数据,请人工处理:" + file.getName());
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("正在进行" + cityName + "的数据填充~");
|
|
|
|
|
//如果源文件或者示例文件是xls格式,则转化为xlsx格式
|
|
|
|
|
String sourceExcel = file.getAbsolutePath();
|
|
|
|
|
sourceExcel = ExcelKit.ConvertXlsToXlsx(sourceExcel);
|
|
|
|
|
//它的最后一个有效数据行,就是这个城市的整体数据汇总
|
|
|
|
|
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel, 4);//此表格有4行表头
|
|
|
|
|
//最后一行的数据就是整个市州的数据
|
|
|
|
|
List<String> rowData = sheetList.getLast();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Row outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
Double allCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("B"))) / 10000;
|
|
|
|
|
Double chengZhenCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("D"))) / 10000;
|
|
|
|
|
Double xiangChunCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("E"))) / 10000;
|
|
|
|
|
Double lv =
|
|
|
|
|
Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("D"))) /
|
|
|
|
|
Integer.parseInt(rowData.get(ExcelKit.transLetter2Num("B")));
|
|
|
|
|
//需要保留两位小数
|
|
|
|
|
lv = Double.parseDouble(String.format("%.2f", lv));
|
|
|
|
|
ExcelKit.putData(outRow, Arrays.asList(cityName, "总人口",Double.parseDouble(String.format("%.2f", allCount))+"" , "万人", "云南省"), dataStyle);
|
|
|
|
|
outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
ExcelKit.putData(outRow, Arrays.asList(cityName, "城镇人口",Double.parseDouble(String.format("%.2f", chengZhenCount))+"", "万人", "云南省"), dataStyle);
|
|
|
|
|
outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
ExcelKit.putData(outRow, Arrays.asList(cityName, "乡村人口", Double.parseDouble(String.format("%.2f", xiangChunCount))+"", "万人", "云南省"), dataStyle);
|
|
|
|
|
outRow = outSheet.createRow(++rowIndex);
|
|
|
|
|
ExcelKit.putData(outRow, Arrays.asList(cityName, "城镇化率", lv + "%", "", "云南省"), dataStyle);
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
System.out.println("错误数据:");
|
|
|
|
|
System.out.println(rowData);
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//保存文件
|
|
|
|
|
ExcelKit.saveExcel(excelPath, outWorkbook);
|
|
|
|
|
System.out.println("市州所有文件处理完成!");
|
|
|
|
|
}
|
|
|
|
|
}
|