|
|
|
@ -27,8 +27,12 @@ public class C1 {
|
|
|
|
|
//实例化
|
|
|
|
|
ReadDocxUtil ru = new ReadDocxUtil();
|
|
|
|
|
|
|
|
|
|
//开始读取市州word文档
|
|
|
|
|
String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
|
|
|
|
|
|
|
|
|
|
//示例Excel
|
|
|
|
|
String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\学前教育入园总量变化及预测.xlsx";
|
|
|
|
|
|
|
|
|
|
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
|
|
|
|
|
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
|
|
|
|
|
ExcelKit.delExcel(excelPath);
|
|
|
|
@ -36,26 +40,12 @@ public class C1 {
|
|
|
|
|
//目标Excel
|
|
|
|
|
XSSFWorkbook outWorkbook = new XSSFWorkbook();
|
|
|
|
|
//目标Sheet
|
|
|
|
|
XSSFSheet outSheet = outWorkbook.createSheet("Sheet1");
|
|
|
|
|
XSSFSheet outSheet = ExcelKit.CreateSheet(outWorkbook);
|
|
|
|
|
//应用样式
|
|
|
|
|
XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook);
|
|
|
|
|
XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook);
|
|
|
|
|
|
|
|
|
|
//先把样例的第一行复制到目标输出Excel的第一行中去
|
|
|
|
|
Row firstRow = outSheet.createRow(0);
|
|
|
|
|
//样例Excel
|
|
|
|
|
XSSFWorkbook sampleWorkbook = new XSSFWorkbook(sampleExcelPath);
|
|
|
|
|
//样例Sheet
|
|
|
|
|
XSSFSheet sampleSheet = sampleWorkbook.getSheet("Sheet1");
|
|
|
|
|
Row firstSampleRow = sampleSheet.getRow(0);
|
|
|
|
|
for (Cell cell : firstSampleRow) {
|
|
|
|
|
Cell outCell = firstRow.createCell(cell.getColumnIndex());
|
|
|
|
|
outCell.setCellValue(ExcelReader.readCell(cell));
|
|
|
|
|
outCell.setCellStyle(headerStyle); // 应用加粗样式
|
|
|
|
|
}
|
|
|
|
|
sampleWorkbook.close();
|
|
|
|
|
|
|
|
|
|
//开始读取市州word文档
|
|
|
|
|
String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
|
|
|
|
|
//拷贝文件头,第一行
|
|
|
|
|
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
|
|
|
|
|
|
|
|
|
|
//找到parentPath下一级目录中所有文件
|
|
|
|
|
File[] files = new File(parentPath).listFiles();
|
|
|
|
@ -80,141 +70,51 @@ public class C1 {
|
|
|
|
|
List<XWPFChart> charts = doc.getCharts();
|
|
|
|
|
|
|
|
|
|
//1号模板,数据在图表5和28中
|
|
|
|
|
XWPFChart chart5 = charts.get(4);
|
|
|
|
|
XSSFWorkbook workbook = chart5.getWorkbook();
|
|
|
|
|
XSSFSheet sheet = workbook.getSheet("Sheet1");
|
|
|
|
|
//遍历输出sheet的内容
|
|
|
|
|
XWPFChart chart = charts.get(4);
|
|
|
|
|
XSSFWorkbook workbook = chart.getWorkbook();
|
|
|
|
|
// 要读取的数据源
|
|
|
|
|
List<List<String>> source1 = ExcelKit.readSheet(workbook);
|
|
|
|
|
|
|
|
|
|
chart = charts.get(27);
|
|
|
|
|
workbook = chart.getWorkbook();
|
|
|
|
|
// 要读取的数据源
|
|
|
|
|
List<List<String>> source2 = ExcelKit.readSheet(workbook);
|
|
|
|
|
|
|
|
|
|
//遍历source1
|
|
|
|
|
int rowIndex = 0;
|
|
|
|
|
Row outRow;
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
for (int i = 0; i < source1.size(); i++) {
|
|
|
|
|
List<String> r = source1.get(i);
|
|
|
|
|
rowIndex++;
|
|
|
|
|
if (rowIndex == 1) continue;
|
|
|
|
|
//年份
|
|
|
|
|
int columnIndex = 0;
|
|
|
|
|
String value = ExcelReader.readCell(row.getCell(columnIndex));
|
|
|
|
|
outRow = outSheet.createRow(rowIndex - 1);
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(value);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//总量分类
|
|
|
|
|
columnIndex = 1;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("总入园数");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//区域分类
|
|
|
|
|
columnIndex = 2;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
|
|
|
|
|
//总量数值
|
|
|
|
|
columnIndex = 3;
|
|
|
|
|
value = ExcelReader.readCell(row.getCell(4));
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(value);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
|
|
|
|
|
//区域数值
|
|
|
|
|
columnIndex = 4;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
|
|
|
|
|
//行政区划
|
|
|
|
|
columnIndex = 5;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(cityName);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//上级行政区划
|
|
|
|
|
columnIndex = 6;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("云南省");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
Row outRow = outSheet.createRow(rowIndex);
|
|
|
|
|
// 需要写死列的对应关系
|
|
|
|
|
outRow.createCell(0).setCellValue(r.getFirst());//年份
|
|
|
|
|
outRow.createCell(1).setCellValue("总入园数");//总量分类
|
|
|
|
|
outRow.createCell(2).setCellValue("");//区域分类
|
|
|
|
|
outRow.createCell(3).setCellValue(r.get(4));//总量数值
|
|
|
|
|
outRow.createCell(4).setCellValue("");//区域数值
|
|
|
|
|
outRow.createCell(5).setCellValue(cityName);//行政区划
|
|
|
|
|
outRow.createCell(6).setCellValue("云南省");//上级行政区划
|
|
|
|
|
//应用一下样式
|
|
|
|
|
ExcelKit.applyStyle(outRow, 0, 6, dataStyle);
|
|
|
|
|
}
|
|
|
|
|
workbook.close();
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
//读取第28个图表
|
|
|
|
|
XWPFChart chart28 = charts.get(27);
|
|
|
|
|
workbook = chart28.getWorkbook();
|
|
|
|
|
sheet = workbook.getSheet("Sheet1");
|
|
|
|
|
String jiShu = "";
|
|
|
|
|
//遍历输出sheet的内容
|
|
|
|
|
int secondRowIndex = 0;
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
secondRowIndex++;
|
|
|
|
|
|
|
|
|
|
if (secondRowIndex == 1) continue;//表头都跳过
|
|
|
|
|
if (secondRowIndex == 2) {//2022年是重复的,取一回就行了
|
|
|
|
|
jiShu = ExcelReader.readCell(row.getCell(4));//2022年基数
|
|
|
|
|
}
|
|
|
|
|
//遍历source2
|
|
|
|
|
for (int i = 0; i < source2.size(); i++) {
|
|
|
|
|
if (i == 0) continue;//跳过2022年,这个年份重复了
|
|
|
|
|
List<String> r = source2.get(i);
|
|
|
|
|
rowIndex++;
|
|
|
|
|
//年份
|
|
|
|
|
int columnIndex = 0;
|
|
|
|
|
if (row.getCell(columnIndex) == null) break;
|
|
|
|
|
String value = ExcelReader.readCell(row.getCell(columnIndex));
|
|
|
|
|
outRow = outSheet.createRow(rowIndex - 1);
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(value);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//总量分类
|
|
|
|
|
columnIndex = 1;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("总入园数");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//区域分类
|
|
|
|
|
columnIndex = 2;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//总量数值
|
|
|
|
|
columnIndex = 3;
|
|
|
|
|
value = ExcelReader.readCell(row.getCell(4));
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(value);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//区域数值
|
|
|
|
|
columnIndex = 4;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//行政区划
|
|
|
|
|
columnIndex = 5;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(cityName);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//上级行政区划
|
|
|
|
|
columnIndex = 6;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("云南省");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
Row outRow = outSheet.createRow(rowIndex);
|
|
|
|
|
// 需要写死列的对应关系
|
|
|
|
|
outRow.createCell(0).setCellValue(r.getFirst());//年份
|
|
|
|
|
outRow.createCell(1).setCellValue("总入园数");//总量分类
|
|
|
|
|
outRow.createCell(2).setCellValue("");//区域分类
|
|
|
|
|
outRow.createCell(3).setCellValue(r.get(4));//总量数值
|
|
|
|
|
outRow.createCell(4).setCellValue("");//区域数值
|
|
|
|
|
outRow.createCell(5).setCellValue(cityName);//行政区划
|
|
|
|
|
outRow.createCell(6).setCellValue("云南省");//上级行政区划
|
|
|
|
|
//应用一下样式
|
|
|
|
|
ExcelKit.applyStyle(outRow, 0, 6, dataStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//重复读取,跳过表头
|
|
|
|
|
rowIndex--;
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
//年份
|
|
|
|
|
int columnIndex = 0;
|
|
|
|
|
if (row.getCell(columnIndex) == null) break;
|
|
|
|
|
String value = ExcelReader.readCell(row.getCell(columnIndex));
|
|
|
|
|
if (StrKit.isBlank(value)) continue;
|
|
|
|
|
rowIndex++;
|
|
|
|
|
outRow = outSheet.createRow(rowIndex - 1);
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(value);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//总量分类
|
|
|
|
|
columnIndex = 1;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("2022年基数");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//区域分类
|
|
|
|
|
columnIndex = 2;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//总量数值
|
|
|
|
|
columnIndex = 3;
|
|
|
|
|
value = ExcelReader.readCell(row.getCell(4));
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(jiShu);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//区域数值
|
|
|
|
|
columnIndex = 4;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//行政区划
|
|
|
|
|
columnIndex = 5;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue(cityName);
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
//上级行政区划
|
|
|
|
|
columnIndex = 6;
|
|
|
|
|
outRow.createCell(columnIndex).setCellValue("云南省");
|
|
|
|
|
outRow.getCell(columnIndex).setCellStyle(dataStyle);
|
|
|
|
|
}
|
|
|
|
|
workbook.close();
|
|
|
|
|
|
|
|
|
|
//城区
|
|
|
|
|
|
|
|
|
@ -225,9 +125,6 @@ public class C1 {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//保存Excel
|
|
|
|
|
outSheet.setDefaultColumnWidth(20);
|
|
|
|
|
outSheet.setDefaultRowHeight((short) 500);
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(excelPath);
|
|
|
|
|
outWorkbook.write(fileOut);
|
|
|
|
|
outWorkbook.close();
|
|
|
|
|