kgdxpr 8 months ago
commit 8a6929d83b

@ -19,10 +19,10 @@ import java.util.List;
public class A1 {
//示例Excel
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【8】人口总体情况\\人口总体情况.xlsx";
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【1】学前幼儿入园总量预测\\学前幼儿入园总量预测.xlsx";
//源文件
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\2023年数据更新表\\16州市数据更新表汇总20241021";
static String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告";
public static void main(String[] args) throws IOException, InvalidFormatException {
//初始化数据库连接
@ -50,28 +50,111 @@ public class A1 {
//找到parentPath下一级目录中所有文件
List<File> files = FileUtil.loopFiles(parentPath, file -> true);
int rowIndex = 0;
String[] excludeCityList = {"~$", "磨憨-磨丁", "经开区", "阳宗海"};
//处理这个目录
if (files != null) {
for (File file : files) {
//判断file是不是目录是目录的需要跳过
if (file.isDirectory()) continue;
//城市名称
String cityName = ru.getCityOrAreaName(file.getName());
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;
//只关心发展规模数据的表格
if (!file.getName().contains("发展规模数据")) continue;
//县区名称
String areaName = ru.getCityOrAreaName(file.getName());
if (StrKit.isBlank(areaName)) {
System.out.println("发现异常数据,请人工处理:" + file.getName());
System.exit(0);
}
//市州名称
//此文件的路径中最后一层的目录名称,应该是带着市州名称 ,把最后一层子目录的名称提出
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)) {
System.out.println("发现异常数据,请人工处理:" + file.getName());
System.exit(0);
}
System.out.println("正在进行" + cityName + "的数据填充~");
//如果源文件或者示例文件是xls格式则转化为xlsx格式
//县区名称
System.out.println("正在进行" + cityName + "-" + areaName + "的数据填充~");
String sourceExcel = file.getAbsolutePath();
List<List<String>> dataList=ExcelKit.readSecondTable(sourceExcel, 0, "自动计算招生数、在校生数",
List<List<String>> dataList = ExcelKit.readSecondTable(sourceExcel, 0, "自动计算招生数、在校生数",
2, "K");
System.out.println(dataList);
//入园总数
for (List<String> stringList : dataList) {
//年份
if (stringList.isEmpty() || StrKit.isBlank(stringList.get(0))) break;
int year = Integer.parseInt(stringList.get(0));
//3预测总招生数
int zss = Integer.parseInt(stringList.get(3).split("\\.")[0]);
Row outRow = outSheet.createRow(++rowIndex);
ExcelKit.putData(outRow, Arrays.asList(String.valueOf(year),
"总入园数", "", String.valueOf(zss), "", areaName, cityName), dataStyle);
}
//2022入园基数
// for (List<String> ignored : dataList) {
// //年份
// int year = 2022;
// //3预测总招生数
// int zss = Integer.parseInt(dataList.getFirst().get(3).split("\\.")[0]);
// Row outRow = outSheet.createRow(++rowIndex);
// ExcelKit.putData(outRow, Arrays.asList(String.valueOf(year),
// "2022年基数", "", String.valueOf(zss), "", areaName, cityName), dataStyle);
// }
//
// //城区
// for (List<String> stringList : dataList) {
// //年份
// int year = Integer.parseInt(stringList.get(0));
// //4修正城区招生A
// int v = Integer.parseInt(stringList.get(4).split("\\.")[0]);
// Row outRow = outSheet.createRow(++rowIndex);
// ExcelKit.putData(outRow, Arrays.asList(String.valueOf(year),
// "", "城区", "", String.valueOf(v), areaName, cityName), dataStyle);
//
// }
//
// //镇区
// for (List<String> stringList : dataList) {
// //年份
// int year = Integer.parseInt(stringList.get(0));
//
// int v = Integer.parseInt(stringList.get(5).split("\\.")[0]);
// Row outRow = outSheet.createRow(++rowIndex);
// ExcelKit.putData(outRow, Arrays.asList(String.valueOf(year),
// "", "镇区", "", String.valueOf(v), areaName, cityName), dataStyle);
//
// }
//
// //乡村
// for (List<String> stringList : dataList) {
// //年份
// int year = Integer.parseInt(stringList.get(0));
// int v = Integer.parseInt(stringList.get(5).split("\\.")[0]);
// Row outRow = outSheet.createRow(++rowIndex);
// ExcelKit.putData(outRow, Arrays.asList(String.valueOf(year),
// "", "乡村", "", String.valueOf(v), areaName, cityName), dataStyle);
//
// }
}
}
//保存文件
ExcelKit.saveExcel(excelPath, outWorkbook);
System.out.println("市州所有文件处理完成!");
System.out.println("县区所有文件处理完成!");
}
}

@ -138,13 +138,14 @@ public class C7 {
sourceExcel = ExcelKit.ConvertXlsToXlsx(sourceExcel);
//它的最后一个有效数据行,就是这个城市的整体数据汇总
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel, 4);//此表格有4行表头
//最后一行的数据就是整个市州的数据
List<String> rowData = sheetList.getLast();
//一行的数据就是整个市州的数据
List<String> rowData = sheetList.getFirst();
for (String s : a) {
for (String t : b) {
Row outRow = outSheet.createRow(++rowIndex);
try {
ExcelKit.putData(outRow, Arrays.asList(cityName, t, "2023", s, rowData.get(ExcelKit.transLetter2Num(schoolData.get(s + "_" + t))), "云南省"), dataStyle);
ExcelKit.putData(outRow, Arrays.asList(cityName, t, "2023", s,
rowData.get(ExcelKit.transLetter2Num(schoolData.get(s + "_" + t))), "云南省"), dataStyle);
} catch (Exception err) {
System.out.println("错误数据:");
System.out.println(rowData);

@ -69,8 +69,8 @@ public class C8 {
sourceExcel = ExcelKit.ConvertXlsToXlsx(sourceExcel);
//它的最后一个有效数据行,就是这个城市的整体数据汇总
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel, 4);//此表格有4行表头
//最后一行的数据就是整个市州的数据
List<String> rowData = sheetList.getLast();
//一行的数据就是整个市州的数据
List<String> rowData = sheetList.getFirst();
try {
Row outRow = outSheet.createRow(++rowIndex);

@ -176,56 +176,62 @@ public class ExcelKit {
workbook.close();
return array;
}
public static String readCell(Cell cell){
return readCell(cell,"");
}
/**
*
*
* @param cell
*/
public static String readCell(Cell cell) {
// 根据单元格的类型来读取值
switch (cell.getCellType()) {
case STRING:
// 字符串类型的单元格
return cell.getStringCellValue();
case NUMERIC:
// 数字类型的单元格
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
// 判断是否为整数
double value = cell.getNumericCellValue();
if (value == (int) value) {
// 整数
return String.valueOf((int) value);
public static String readCell(Cell cell,String defaultValue) {
try {
// 根据单元格的类型来读取值
switch (cell.getCellType()) {
case STRING:
// 字符串类型的单元格
return cell.getStringCellValue();
case NUMERIC:
// 数字类型的单元格
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
// 小数,保留两位小数
value = cell.getNumericCellValue();
return String.format("%.2f", value);
// 判断是否为整数
double value = cell.getNumericCellValue();
if (value == (int) value) {
// 整数
return String.valueOf((int) value);
} else {
// 小数,保留两位小数
value = cell.getNumericCellValue();
return String.format("%.2f", value);
}
}
}
case FORMULA:
// 公式类型的单元格
cell.setCellType(CellType.NUMERIC); // 将公式计算结果转为数字
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
double formulaValue = cell.getNumericCellValue();
if (formulaValue == (int) formulaValue) {
// 整数
return String.valueOf((int) formulaValue);
case FORMULA:
// 公式类型的单元格
cell.setCellType(CellType.NUMERIC); // 将公式计算结果转为数字
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
// 小数,保留两位小数
double value = cell.getNumericCellValue();
return String.format("%.2f", value);
double formulaValue = cell.getNumericCellValue();
if (formulaValue == (int) formulaValue) {
// 整数
return String.valueOf((int) formulaValue);
} else {
// 小数,保留两位小数
double value = cell.getNumericCellValue();
return String.format("%.2f", value);
}
}
}
// 可以添加其他类型的处理,如 BOOLEAN, ERROR 等
default:
// 其他类型的单元格
return cell.toString();
// 可以添加其他类型的处理,如 BOOLEAN, ERROR 等
default:
// 其他类型的单元格
return cell.toString();
}
}catch (Exception err){
return defaultValue;
}
}
@ -543,13 +549,18 @@ public class ExcelKit {
}
}
start += headRows;//第二个表的表头有几行
System.out.println("第二个表格开始行索引=" + start + ",结束行索引=" + end);
//System.out.println("第二个表格开始行索引=" + start + ",结束行索引=" + end);
//输出第二个表格的数据
for (rowIndex = start; rowIndex <= end; rowIndex++) {
Row row = sheet.getRow(rowIndex);
List<String> rowData = new ArrayList<>();
for (int j = 0; j < tableWidth; j++) {
Cell cell = row.getCell(j);
if (j == 0 && (cell == null || StrKit.isBlank(readCell(cell)))) break;
if (cell == null) {
rowData.add(null);
continue;
}
// 如果单元格是公式类型
if (cell.getCellType() == CellType.FORMULA) {
// 计算公式结果
@ -563,4 +574,13 @@ public class ExcelKit {
fis.close();
return dataList;
}
public static void printTable(List<List<String>> dataList) {
for (List<String> row : dataList) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
}

@ -13,7 +13,7 @@ public class TestRead {
public static void main(String[] args) throws IOException {
//待读取的EXCEL文件
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【1】学前幼儿入园总量预测\\鲁甸县教育发展规模数据收集表.xlsx";
String filePath = "C:\\Users\\Administrator\\全省及州市县区人口与教育报告集20241023\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\文山州各县市报告8\\马关县\\马关县教育发展规模数据收集表-20240418.xlsx";
//表格正文上方的文字
String keyword = "自动计算招生数、在校生数";

@ -17,6 +17,7 @@ public class ReadDocxUtil {
public DataEaseModel dm = new DataEaseModel();
public String privinceName = "云南省";
/**
*
*

Loading…
Cancel
Save