main
黄海 9 months ago
parent a761c200e4
commit 33f220e399

@ -50,6 +50,7 @@ public class A1 {
//找到parentPath下一级目录中所有文件
List<File> files = FileUtil.loopFiles(parentPath, file -> true);
int rowIndex = 0;
String[] excludeCityList = {"~$", "磨憨-磨丁", "经开区", "阳宗海"};
//处理这个目录
if (files != null) {
for (File file : files) {
@ -57,6 +58,16 @@ public class A1 {
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;
//只关心发展规模数据的表格
if (!file.getName().contains("发展规模数据")) continue;
//县区名称
String areaName = ru.getCityOrAreaName(file.getName());
@ -87,6 +98,7 @@ public class A1 {
//入园总数
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]);
@ -95,54 +107,54 @@ public class A1 {
"总入园数", "", 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);
}
// 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("县区所有文件处理完成!");
}
}

@ -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;
}
}
@ -550,6 +556,7 @@ public class ExcelKit {
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;

@ -13,7 +13,7 @@ public class TestRead {
public static void main(String[] args) throws IOException {
//待读取的EXCEL文件
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\临沧市各县区报告8\\云县教育体育局关于做好人口变化趋势对基础教育影响研究工作材料\\表2云县教育发展规模数据收集表-20240422-镇乡两类适用.xlsx";
String filePath = "C:\\Users\\Administrator\\全省及州市县区人口与教育报告集20241023\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\文山州各县市报告8\\马关县\\马关县教育发展规模数据收集表-20240418.xlsx";
//表格正文上方的文字
String keyword = "自动计算招生数、在校生数";

Loading…
Cancel
Save