diff --git a/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测.xlsx b/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测.xlsx index 4fc45ab8..3c8ca98d 100644 Binary files a/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测.xlsx and b/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测.xlsx differ diff --git a/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测【成果】.xlsx b/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测【成果】.xlsx index 264b9a6f..9bf17d0e 100644 Binary files a/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测【成果】.xlsx and b/Doc/待处理/市/【1】学前教育入园总量变化及预测/学前教育入园总量变化及预测【成果】.xlsx differ diff --git a/src/main/java/com/dsideal/base/Tools/FillData/City/C1.java b/src/main/java/com/dsideal/base/Tools/FillData/City/C1.java index 292d27c2..68a35e87 100644 --- a/src/main/java/com/dsideal/base/Tools/FillData/City/C1.java +++ b/src/main/java/com/dsideal/base/Tools/FillData/City/C1.java @@ -1,29 +1,29 @@ package com.dsideal.base.Tools.FillData.City; import com.dsideal.base.DataEase.Model.ExcelReader; +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.openxml4j.util.ZipSecureFile; import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFChart; import org.apache.poi.xwpf.usermodel.XWPFDocument; -import org.apache.poi.xwpf.usermodel.XWPFParagraph; import java.io.*; -import java.util.ArrayList; import java.util.List; public class C1 { + public static void main(String[] args) throws IOException, InvalidFormatException { //初始化数据库连接 LocalMysqlConnectUtil.Init(); + //实例化 ReadDocxUtil ru = new ReadDocxUtil(); @@ -31,37 +31,14 @@ public class C1 { String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\学前教育入园总量变化及预测.xlsx"; //目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死 String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx"); - //如果目录文件存在,则删除 - File fi = new File(excelPath); - if (fi.exists()) { - fi.delete(); - } + ExcelKit.delExcel(excelPath); //目标Excel XSSFWorkbook outWorkbook = new XSSFWorkbook(); //目标Sheet XSSFSheet outSheet = outWorkbook.createSheet("Sheet1"); - XSSFCellStyle headerStyle = outWorkbook.createCellStyle(); - XSSFFont font = outWorkbook.createFont(); - font.setBold(true); // 设置字体加粗 - headerStyle.setFont(font); - // 设置边框样式 - headerStyle.setBorderTop(BorderStyle.THIN); - headerStyle.setBorderBottom(BorderStyle.THIN); - headerStyle.setBorderLeft(BorderStyle.THIN); - headerStyle.setBorderRight(BorderStyle.THIN); - // 设置水平和垂直居中 - headerStyle.setAlignment(HorizontalAlignment.CENTER); - headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); - - XSSFCellStyle dataStyle = outWorkbook.createCellStyle(); - dataStyle.setBorderTop(BorderStyle.THIN); // 上边框 - dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框 - dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框 - dataStyle.setBorderRight(BorderStyle.THIN); // 右边框 - // 设置水平和垂直居中 - dataStyle.setAlignment(HorizontalAlignment.CENTER); - dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); + XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook); + XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook); //先把样例的第一行复制到目标输出Excel的第一行中去 Row firstRow = outSheet.createRow(0); @@ -77,7 +54,6 @@ public class C1 { } sampleWorkbook.close(); - //开始读取市州word文档 String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510"; @@ -155,15 +131,20 @@ public class C1 { 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 || secondRowIndex == 2) continue;//表头和2022年都跳过 + + if (secondRowIndex == 1) continue;//表头都跳过 + if (secondRowIndex == 2) {//2022年是重复的,取一回就行了 + jiShu = ExcelReader.readCell(row.getCell(4));//2022年基数 + } rowIndex++; //年份 int columnIndex = 0; - if(row.getCell(columnIndex)==null) break; + if (row.getCell(columnIndex) == null) break; String value = ExcelReader.readCell(row.getCell(columnIndex)); outRow = outSheet.createRow(rowIndex - 1); outRow.createCell(columnIndex).setCellValue(value); @@ -194,17 +175,59 @@ public class C1 { outRow.createCell(columnIndex).setCellValue("云南省"); outRow.getCell(columnIndex).setCellStyle(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(); + + //城区 + + //镇区 + + //乡村 break; } } } //保存Excel - //设置行的高度=28,列的宽度为自动 outSheet.setDefaultColumnWidth(20); outSheet.setDefaultRowHeight((short) 500); - - FileOutputStream fileOut = new FileOutputStream(excelPath); outWorkbook.write(fileOut); outWorkbook.close(); diff --git a/src/main/java/com/dsideal/base/Tools/FillData/ExcelKit/ExcelKit.java b/src/main/java/com/dsideal/base/Tools/FillData/ExcelKit/ExcelKit.java new file mode 100644 index 00000000..4897e565 --- /dev/null +++ b/src/main/java/com/dsideal/base/Tools/FillData/ExcelKit/ExcelKit.java @@ -0,0 +1,63 @@ +package com.dsideal.base.Tools.FillData.ExcelKit; + +import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.File; + +public class ExcelKit { + /** + * 获取表头样式 + * + * @param workbook 指定的工作簿 + * @return + */ + public static XSSFCellStyle getHeaderStyle(XSSFWorkbook workbook) { + XSSFCellStyle headerStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setBold(true); // 设置字体加粗 + headerStyle.setFont(font); + // 设置边框样式 + headerStyle.setBorderTop(BorderStyle.THIN); + headerStyle.setBorderBottom(BorderStyle.THIN); + headerStyle.setBorderLeft(BorderStyle.THIN); + headerStyle.setBorderRight(BorderStyle.THIN); + // 设置水平和垂直居中 + headerStyle.setAlignment(HorizontalAlignment.CENTER); + headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); + return headerStyle; + } + + /** + * 获取数据样式 + * @param workbook 指定的工作簿 + * @return + */ + public static XSSFCellStyle getDataStyle(XSSFWorkbook workbook) { + XSSFCellStyle dataStyle = workbook.createCellStyle(); + dataStyle.setBorderTop(BorderStyle.THIN); // 上边框 + dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框 + dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框 + dataStyle.setBorderRight(BorderStyle.THIN); // 右边框 + // 设置水平和垂直居中 + dataStyle.setAlignment(HorizontalAlignment.CENTER); + dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); + return dataStyle; + } + + /** + * 删除Excel文件 + * @param excelPath + */ + public static void delExcel(String excelPath){ + //如果目录文件存在,则删除 + File fi = new File(excelPath); + if (fi.exists()) { + fi.delete(); + } + } +}