commit 1c4f91635bf59313ee36e01ea0eb79cd0110be70 Author: chenda Date: Mon Nov 7 09:54:11 2022 +0800 updata diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fce67ff --- /dev/null +++ b/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.5 + + + com.gmh + gmh + 0.0.1-SNAPSHOT + gmh + gmh + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.mysql + mysql-connector-j + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.apache.poi + poi + 3.9 + + + + + org.apache.poi + poi-ooxml + 3.9 + + + + cn.hutool + hutool-all + 5.8.1 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/com/gmh/GmhApplication.java b/src/main/java/com/gmh/GmhApplication.java new file mode 100644 index 0000000..a68a505 --- /dev/null +++ b/src/main/java/com/gmh/GmhApplication.java @@ -0,0 +1,13 @@ +package com.gmh; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GmhApplication { + + public static void main(String[] args) { + SpringApplication.run(GmhApplication.class, args); + } + +} diff --git a/src/main/java/com/gmh/test.java b/src/main/java/com/gmh/test.java new file mode 100644 index 0000000..ba1cabf --- /dev/null +++ b/src/main/java/com/gmh/test.java @@ -0,0 +1,125 @@ +package com.gmh; + +import cn.hutool.core.util.StrUtil; +import com.gmh.utils.POIExcelUtil; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author ChenDa + * @ClassName test + * @description: TODO + * @datetime 2022/11/06 16:43 + * @version: 1.0 + */ +public class test { + + public static void main(String[] args) throws IOException { + File file = new File("C:\\Users\\xxche\\Desktop\\继续教育证明\\盛华编制聘任制.xlsx"); + Workbook workbook = POIExcelUtil.readExcelFromFile(file); + // 编制 + Sheet bianzhi = workbook.getSheetAt(0); + + List bianzhiList = new ArrayList<>(); + for (int i = 0; i <= bianzhi.getLastRowNum(); i++) { + Row row = bianzhi.getRow(i); + if (row == null) { + continue; + } + Cell cell = row.getCell(1); + String name = cell.getStringCellValue(); + if ("姓名".equals(name)) { + continue; + } + if (StrUtil.isBlankIfStr(name)) { + continue; + } + bianzhiList.add(nameFormat(name)); + } + Workbook bianzhiWb = POIExcelUtil.batchCloneSheet(new File("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明.xls"), 0, bianzhiList.size()); + for (int i = 0; i < bianzhiWb.getNumberOfSheets(); i++) { + Sheet sheet = bianzhiWb.getSheetAt(i); + if (sheet == null) { + continue; + } + Row row = sheet.getRow(2); + Cell cell = row.getCell(0); + cell.setCellValue(" (2022年度)\n" + + " ${NAME}同志于2022年 1月4日至2022年11月14日参加${schoolName} 举办的继续教育培训,总计 90 学时,成绩合格,特此证明。" + .replace("${NAME}", bianzhiList.get(i)) + .replace("${schoolName}", "长春市盛华学校")); + } + + try { + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明222.xls"); + out.flush(); + bianzhiWb.write(out); + out.close(); + System.out.println("编制文件输出完成"); + } catch (IOException e) { + e.printStackTrace(); + } + + + // 聘任制 + Sheet pinren = workbook.getSheetAt(1); + List pinrenList = new ArrayList<>(); + for (int i = 0; i <= pinren.getLastRowNum(); i++) { + Row row = pinren.getRow(i); + if (row == null) { + continue; + } + Cell cell = row.getCell(1); + String name = cell.getStringCellValue(); + if ("姓名".equals(name)) { + continue; + } + if (StrUtil.isBlankIfStr(name)) { + continue; + } + pinrenList.add(name); + } + Workbook pinrenWb = POIExcelUtil.batchCloneSheet(new File("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制).xls"), 0, pinrenList.size()); + for (int i = 0; i < pinrenWb.getNumberOfSheets(); i++) { + Sheet sheet = pinrenWb.getSheetAt(i); + if (sheet == null) { + continue; + } + Row row = sheet.getRow(2); + Cell cell = row.getCell(0); + cell.setCellValue(" (2022年度)\n" + + " ${NAME}同志于2022年 1月4日至2022年11月14日参加${schoolName} 举办的继续教育培训,总计 90 学时,成绩合格,特此证明。" + .replace("${NAME}", pinrenList.get(i)) + .replace("${schoolName}", "长春市盛华学校")); + } + + try { + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制)222.xls"); + out.flush(); + pinrenWb.write(out); + out.close(); + System.out.println("聘任制文件输出完成"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public static String nameFormat(String name) { + StringBuilder newName = new StringBuilder(name.replaceAll("\\s*", "")); + if (newName.length() == 2) { + newName.insert(1, " "); + return newName.toString(); + } + return name; + + } +} diff --git a/src/main/java/com/gmh/test.txt b/src/main/java/com/gmh/test.txt new file mode 100644 index 0000000..38a14b1 --- /dev/null +++ b/src/main/java/com/gmh/test.txt @@ -0,0 +1,127 @@ +package com.gmh; + +import cn.hutool.core.date.LocalDateTimeUtil; +import cn.hutool.core.util.StrUtil; +import com.gmh.utils.POIExcelUtil; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @author ChenDa + * @ClassName test + * @description: TODO + * @datetime 2022/11/06 16:43 + * @version: 1.0 + */ +public class test { + + public static void main(String[] args) throws IOException { + File file = new File("C:\\Users\\xxche\\Desktop\\继续教育证明\\盛华编制聘任制.xlsx"); + Workbook workbook = POIExcelUtil.readExcelFromFile(file); + // 编制 + Sheet bianzhi = workbook.getSheetAt(0); + + List bianzhiList = new ArrayList<>(); + for (int i = 0; i <= bianzhi.getLastRowNum(); i++) { + Row row = bianzhi.getRow(i); + if (row == null) { + continue; + } + Cell cell = row.getCell(1); + String name = cell.getStringCellValue(); + if ("姓名".equals(name)) { + continue; + } + if (StrUtil.isBlankIfStr(name)) { + continue; + } + bianzhiList.add(nameFormat(name)); + } + Workbook bianzhiWb = POIExcelUtil.batchCloneSheet(new File("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明.xls"), 0, bianzhiList.size()); + for (int i = 0; i < bianzhiWb.getNumberOfSheets(); i++) { + Sheet sheet = bianzhiWb.getSheetAt(i); + if (sheet == null) { + continue; + } + Row row = sheet.getRow(2); + Cell cell = row.getCell(0); + cell.setCellValue(" (2022年度)\n" + + " ${NAME}同志于2022年 1月4日至2022年11月14日参加${schoolName} 举办的继续教育培训,总计 90 学时,成绩合格,特此证明。" + .replace("${NAME}", bianzhiList.get(i)) + .replace("${schoolName}", "长春市盛华学校")); + } + + try { + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明222.xls"); + out.flush(); + bianzhiWb.write(out); + out.close(); + System.out.println("编制文件输出完成"); + } catch (IOException e) { + e.printStackTrace(); + } + + + // 聘任制 + Sheet pinren = workbook.getSheetAt(1); + List pinrenList = new ArrayList<>(); + for (int i = 0; i <= pinren.getLastRowNum(); i++) { + Row row = pinren.getRow(i); + if (row == null) { + continue; + } + Cell cell = row.getCell(1); + String name = cell.getStringCellValue(); + if ("姓名".equals(name)) { + continue; + } + if (StrUtil.isBlankIfStr(name)) { + continue; + } + pinrenList.add(name); + } + Workbook pinrenWb = POIExcelUtil.batchCloneSheet(new File("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制).xls"), 0, pinrenList.size()); + for (int i = 0; i < pinrenWb.getNumberOfSheets(); i++) { + Sheet sheet = pinrenWb.getSheetAt(i); + if (sheet == null) { + continue; + } + Row row = sheet.getRow(2); + Cell cell = row.getCell(0); + cell.setCellValue(" (2022年度)\n" + + " ${NAME}同志于2022年 1月4日至2022年11月14日参加${schoolName} 举办的继续教育培训,总计 90 学时,成绩合格,特此证明。" + .replace("${NAME}", pinrenList.get(i)) + .replace("${schoolName}", "长春市盛华学校")); + } + + try { + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制)222.xls"); + out.flush(); + pinrenWb.write(out); + out.close(); + System.out.println("聘任制文件输出完成"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public static String nameFormat(String name) { + StringBuilder newName = new StringBuilder(name.replaceAll("\\s*", "")); + if (newName.length() == 2) { + newName.insert(1, " "); + return newName.toString(); + } + return name; + + } +} diff --git a/src/main/java/com/gmh/utils/POIExcelUtil.java b/src/main/java/com/gmh/utils/POIExcelUtil.java new file mode 100644 index 0000000..33f14fc --- /dev/null +++ b/src/main/java/com/gmh/utils/POIExcelUtil.java @@ -0,0 +1,94 @@ +package com.gmh.utils; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.*; + +/** + * POI Excel工具类 + * created on 2020/5/28 10:08 + * + * @author puhaiyang + */ +public class POIExcelUtil { + + public static void main(String[] args) throws IOException { + File file = new File("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明.xls"); + Workbook workbook = batchCloneSheet(file, 0, 50); + try { + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明222.xls"); + out.flush(); + workbook.write(out); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static Workbook cloneSheet(File excelFile, String srcSheetName, String destSheetName) throws IOException { + Workbook wb = readExcelFromFile(excelFile); + int index = wb.getSheetIndex(srcSheetName); + return cloneSheet(excelFile, index, destSheetName); + } + + public static Workbook cloneSheet(File excelFile, Integer index, String destSheetName) throws IOException { + Workbook wb = readExcelFromFile(excelFile); + //克隆一个新的sheet + Sheet newSheet = wb.cloneSheet(index); + int sheetIndex = wb.getSheetIndex(newSheet); + wb.setSheetName(sheetIndex, destSheetName); + return wb; + + } + + /** + * 复制多个sheet + * + * @param excelFile + * @param index 起始sheet + * @param total 复制多个sheet + */ + public static Workbook batchCloneSheet(File excelFile, Integer index, Integer total) throws IOException { + Workbook wb = readExcelFromFile(excelFile); + for (int i = 0; i < total; i++) { + //克隆一个新的sheet + Sheet newSheet = wb.cloneSheet(index); + int sheetIndex = wb.getSheetIndex(newSheet); + wb.setSheetName(sheetIndex, String.valueOf(i + 1)); + } + wb.removeSheetAt(index); + return wb; + +// try { +// FileOutputStream out = new FileOutputStream(excelFile); +// out.flush(); +// sheets.write(out); +// out.close(); +// } catch (IOException e) { +// e.printStackTrace(); +// } + } + + //读取excel + public static Workbook readExcelFromFile(File file) throws IOException { + if (file == null) { + return null; + } + String fileName = file.getName(); + String[] split = fileName.split("\\."); + if (split.length == 0 || split.length == 1) { + throw new RuntimeException("文件格式错误"); + } + String suffix = split[split.length - 1]; + if ("xls".equals(suffix)) { + return new HSSFWorkbook(new FileInputStream(file)); + } else if ("xlsx".equals(suffix)) { + return new XSSFWorkbook(new FileInputStream(file)); + } else { + throw new RuntimeException("文件格式错误,不是一个正经的Excel"); + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/test/java/com/gmh/GmhApplicationTests.java b/src/test/java/com/gmh/GmhApplicationTests.java new file mode 100644 index 0000000..810ea56 --- /dev/null +++ b/src/test/java/com/gmh/GmhApplicationTests.java @@ -0,0 +1,13 @@ +package com.gmh; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class GmhApplicationTests { + + @Test + void contextLoads() { + } + +}