master
chenda 2 years ago
commit 1c4f91635b

33
.gitignore vendored

@ -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/

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmh</groupId>
<artifactId>gmh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gmh</name>
<description>gmh</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--xls(03)03版本的-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)07版本的-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -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);
}
}

@ -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<String> 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<String> 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;
}
}

@ -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<String> 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<String> 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;
}
}

@ -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");
}
}
}

@ -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() {
}
}
Loading…
Cancel
Save