From 01fb76f5d7c00e968ceefbb5286ca71319ea1b38 Mon Sep 17 00:00:00 2001 From: lenovo Date: Mon, 7 Nov 2022 17:23:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20=E9=9B=86=E6=88=90MyBatis-Plus=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AF=BC=E5=85=A5=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=90=8D=E5=8D=95=E8=A7=A3=E6=9E=90=E7=9A=84?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 ++ .../com/gmh/controller/ExcelController.java | 81 +++++++++++ .../com/gmh/controller/TestController.java | 32 +++++ src/main/java/com/gmh/dao/GmhUserDao.java | 10 ++ .../java/com/gmh/dao/SysBaijiaxingDao.java | 9 ++ src/main/java/com/gmh/entity/GmhUser.java | 18 +++ src/main/java/com/gmh/entity/R.java | 88 ++++++++++++ .../java/com/gmh/entity/SysBaijiaxing.java | 19 +++ .../java/com/gmh/service/GmhUserService.java | 9 ++ .../com/gmh/service/SysBaijiaxingService.java | 7 + .../gmh/service/impl/GmhUserServiceImpl.java | 11 ++ .../impl/SysBaijiaxingServiceImpl.java | 11 ++ src/main/java/com/gmh/test.java | 1 + src/main/java/com/gmh/test.txt | 127 ------------------ src/main/java/com/gmh/utils/POIExcelUtil.java | 73 +++++----- src/main/resources/application.properties | 6 +- 16 files changed, 351 insertions(+), 159 deletions(-) create mode 100644 src/main/java/com/gmh/controller/ExcelController.java create mode 100644 src/main/java/com/gmh/controller/TestController.java create mode 100644 src/main/java/com/gmh/dao/GmhUserDao.java create mode 100644 src/main/java/com/gmh/dao/SysBaijiaxingDao.java create mode 100644 src/main/java/com/gmh/entity/GmhUser.java create mode 100644 src/main/java/com/gmh/entity/R.java create mode 100644 src/main/java/com/gmh/entity/SysBaijiaxing.java create mode 100644 src/main/java/com/gmh/service/GmhUserService.java create mode 100644 src/main/java/com/gmh/service/SysBaijiaxingService.java create mode 100644 src/main/java/com/gmh/service/impl/GmhUserServiceImpl.java create mode 100644 src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java delete mode 100644 src/main/java/com/gmh/test.txt diff --git a/pom.xml b/pom.xml index fce67ff..744bf97 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,14 @@ hutool-all 5.8.1 + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + diff --git a/src/main/java/com/gmh/controller/ExcelController.java b/src/main/java/com/gmh/controller/ExcelController.java new file mode 100644 index 0000000..8f84fe4 --- /dev/null +++ b/src/main/java/com/gmh/controller/ExcelController.java @@ -0,0 +1,81 @@ +package com.gmh.controller; + +import cn.hutool.core.util.StrUtil; +import com.gmh.entity.GmhUser; +import com.gmh.entity.R; +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 org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.*; + +@RestController +@RequestMapping("/excel") +public class ExcelController { + + @RequestMapping("/t01") + public R test01() { + return R.ok("ok"); + } + + @RequestMapping("/readData") + public R readSourceData(MultipartFile file) throws IOException { + Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); + Integer nameCellIndex = null; + Map> sheetNameList = new LinkedHashMap<>(); + for (int i = 0; i < workbook.getNumberOfSheets(); i++) { + List nameList = new ArrayList<>(); + Sheet sheet = workbook.getSheetAt(i); + for (int r = 0; r <= sheet.getLastRowNum(); r++) { + Row row = sheet.getRow(r); + if (row == null) { + continue; + } + int lastCellNum = row.getLastCellNum(); + for (int c = 0; c < lastCellNum; c++) { + Cell cell = row.getCell(c); + String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell); + if (r == 0) { + // 第一行有姓名列 + if ("姓名".equals(stringCellValue)) { + nameCellIndex = c; + break; + } else { + // 第一列没有姓名的情况 + } + } + } + if (nameCellIndex != null) { + Cell cell = row.getCell(nameCellIndex); + String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell); + if (!"姓名".equals(stringCellValue) && StrUtil.isNotBlank(stringCellValue)) { + GmhUser user = new GmhUser(); + user.setName(stringCellValue); + nameList.add(user); + } + } else { + System.out.println("未找到姓名列"); + } + } + if (!nameList.isEmpty()) { + sheetNameList.put(sheet.getSheetName(), nameList); + } + } + return R.ok().setData(sheetNameList); + } + + @RequestMapping("/template") + public R templateUpload(MultipartFile file) throws IOException { + Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); + + return R.ok(); + } + + +} diff --git a/src/main/java/com/gmh/controller/TestController.java b/src/main/java/com/gmh/controller/TestController.java new file mode 100644 index 0000000..1c08f7b --- /dev/null +++ b/src/main/java/com/gmh/controller/TestController.java @@ -0,0 +1,32 @@ +package com.gmh.controller; + +import com.gmh.entity.GmhUser; +import com.gmh.entity.R; +import com.gmh.entity.SysBaijiaxing; +import com.gmh.service.GmhUserService; +import com.gmh.service.SysBaijiaxingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/test") +public class TestController { + + @Autowired + private GmhUserService gmhUserService; + + @Autowired + private SysBaijiaxingService baijiaxingService; + + + @RequestMapping("/test") + public R test() { + List list = gmhUserService.list(); + return R.ok("success").setData(list); + } + + +} diff --git a/src/main/java/com/gmh/dao/GmhUserDao.java b/src/main/java/com/gmh/dao/GmhUserDao.java new file mode 100644 index 0000000..7aa7d02 --- /dev/null +++ b/src/main/java/com/gmh/dao/GmhUserDao.java @@ -0,0 +1,10 @@ +package com.gmh.dao; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface GmhUserDao extends BaseMapper { + + +} diff --git a/src/main/java/com/gmh/dao/SysBaijiaxingDao.java b/src/main/java/com/gmh/dao/SysBaijiaxingDao.java new file mode 100644 index 0000000..396c254 --- /dev/null +++ b/src/main/java/com/gmh/dao/SysBaijiaxingDao.java @@ -0,0 +1,9 @@ +package com.gmh.dao; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.gmh.entity.SysBaijiaxing; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface SysBaijiaxingDao extends BaseMapper { +} diff --git a/src/main/java/com/gmh/entity/GmhUser.java b/src/main/java/com/gmh/entity/GmhUser.java new file mode 100644 index 0000000..d177f52 --- /dev/null +++ b/src/main/java/com/gmh/entity/GmhUser.java @@ -0,0 +1,18 @@ +package com.gmh.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import static com.baomidou.mybatisplus.annotation.IdType.AUTO; + +@Data +@TableName("gmh_user") +public class GmhUser { + + @TableId(type = AUTO) + private Integer id; + + private String name; + +} diff --git a/src/main/java/com/gmh/entity/R.java b/src/main/java/com/gmh/entity/R.java new file mode 100644 index 0000000..5171d11 --- /dev/null +++ b/src/main/java/com/gmh/entity/R.java @@ -0,0 +1,88 @@ +package com.gmh.entity; + +import lombok.Getter; + +import java.io.Serializable; + +@Getter +public class R implements Serializable { + + private static final long serialVersionUID = 1L; + + private boolean success; + private int code; + private String msg; + private Object data; + private long timestamp; + + private R() {} + + public static R error() { + return error(null); + } + + public static R error(String message) { + return error(null, message); + } + + public static R error(Integer code, String message) { + if(code == null) { + code = 500; + } + if(message == null) { + message = "服务器内部错误"; + } + return build(code, false, message); + } + + public static R ok() { + return ok(null); + } + + public static R ok(String message) { + return ok(null, message); + } + + public static R ok(Integer code, String message) { + if(code == null) { + code = 200; + } + if(message == null) { + message = "操作成功"; + } + return build(code, true, message); + } + + public static R build(int code, boolean success, String message) { + return new R() + .setCode(code) + .setSuccess(success) + .setMessage(message) + .setTimestamp(System.currentTimeMillis()); + } + + public R setCode(int code) { + this.code = code; + return this; + } + + public R setSuccess(boolean success) { + this.success = success; + return this; + } + public R setMessage(String msg) { + this.msg = msg; + return this; + } + + public R setData(Object data) { + this.data = data; + return this; + } + + public R setTimestamp(long timestamp) { + this.timestamp = timestamp; + return this; + } + +} diff --git a/src/main/java/com/gmh/entity/SysBaijiaxing.java b/src/main/java/com/gmh/entity/SysBaijiaxing.java new file mode 100644 index 0000000..fdc67f6 --- /dev/null +++ b/src/main/java/com/gmh/entity/SysBaijiaxing.java @@ -0,0 +1,19 @@ +package com.gmh.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import static com.baomidou.mybatisplus.annotation.IdType.AUTO; + +@Data +@TableName("sys_firstname") +public class SysBaijiaxing { + + @TableId(type = AUTO) + private Integer id; + + private String xingshi; + + private String pinyin; +} diff --git a/src/main/java/com/gmh/service/GmhUserService.java b/src/main/java/com/gmh/service/GmhUserService.java new file mode 100644 index 0000000..1983814 --- /dev/null +++ b/src/main/java/com/gmh/service/GmhUserService.java @@ -0,0 +1,9 @@ +package com.gmh.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.gmh.entity.GmhUser; + +public interface GmhUserService extends IService { + + +} diff --git a/src/main/java/com/gmh/service/SysBaijiaxingService.java b/src/main/java/com/gmh/service/SysBaijiaxingService.java new file mode 100644 index 0000000..298480e --- /dev/null +++ b/src/main/java/com/gmh/service/SysBaijiaxingService.java @@ -0,0 +1,7 @@ +package com.gmh.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.gmh.entity.SysBaijiaxing; + +public interface SysBaijiaxingService extends IService { +} diff --git a/src/main/java/com/gmh/service/impl/GmhUserServiceImpl.java b/src/main/java/com/gmh/service/impl/GmhUserServiceImpl.java new file mode 100644 index 0000000..1453680 --- /dev/null +++ b/src/main/java/com/gmh/service/impl/GmhUserServiceImpl.java @@ -0,0 +1,11 @@ +package com.gmh.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.gmh.dao.GmhUserDao; +import com.gmh.entity.GmhUser; +import com.gmh.service.GmhUserService; +import org.springframework.stereotype.Service; + +@Service +public class GmhUserServiceImpl extends ServiceImpl implements GmhUserService { +} diff --git a/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java b/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java new file mode 100644 index 0000000..5ddac19 --- /dev/null +++ b/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java @@ -0,0 +1,11 @@ +package com.gmh.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.gmh.dao.SysBaijiaxingDao; +import com.gmh.entity.SysBaijiaxing; +import com.gmh.service.SysBaijiaxingService; +import org.springframework.stereotype.Service; + +@Service +public class SysBaijiaxingServiceImpl extends ServiceImpl implements SysBaijiaxingService { +} diff --git a/src/main/java/com/gmh/test.java b/src/main/java/com/gmh/test.java index ba1cabf..6c531cf 100644 --- a/src/main/java/com/gmh/test.java +++ b/src/main/java/com/gmh/test.java @@ -44,6 +44,7 @@ public class test { } 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); diff --git a/src/main/java/com/gmh/test.txt b/src/main/java/com/gmh/test.txt deleted file mode 100644 index 38a14b1..0000000 --- a/src/main/java/com/gmh/test.txt +++ /dev/null @@ -1,127 +0,0 @@ -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 index 33f14fc..1801e84 100644 --- a/src/main/java/com/gmh/utils/POIExcelUtil.java +++ b/src/main/java/com/gmh/utils/POIExcelUtil.java @@ -1,11 +1,14 @@ package com.gmh.utils; +import cn.hutool.core.util.StrUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; +import java.nio.file.Files; /** * POI Excel工具类 @@ -28,67 +31,75 @@ public class POIExcelUtil { } } - 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(Workbook workbook, String srcSheetName, String destSheetName) { + int index = workbook.getSheetIndex(srcSheetName); + return cloneSheet(workbook, index, destSheetName); } - public static Workbook cloneSheet(File excelFile, Integer index, String destSheetName) throws IOException { - Workbook wb = readExcelFromFile(excelFile); + public static Workbook cloneSheet(Workbook workbook, Integer index, String destSheetName) { //克隆一个新的sheet - Sheet newSheet = wb.cloneSheet(index); - int sheetIndex = wb.getSheetIndex(newSheet); - wb.setSheetName(sheetIndex, destSheetName); - return wb; + Sheet newSheet = workbook.cloneSheet(index); + int sheetIndex = workbook.getSheetIndex(newSheet); + workbook.setSheetName(sheetIndex, destSheetName); + return workbook; + } + public static Workbook cloneSheet(File excelFile, Integer index, String destSheetName) throws IOException { + Workbook workbook = readExcelFromFile(excelFile); + return cloneSheet(workbook, index, destSheetName); } /** * 复制多个sheet * - * @param excelFile + * @param workbook workbook * @param index 起始sheet * @param total 复制多个sheet */ - public static Workbook batchCloneSheet(File excelFile, Integer index, Integer total) throws IOException { - Workbook wb = readExcelFromFile(excelFile); + public static Workbook batchCloneSheet(Workbook workbook, Integer index, Integer total) throws IOException { 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)); + Sheet newSheet = workbook.cloneSheet(index); + int sheetIndex = workbook.getSheetIndex(newSheet); + workbook.setSheetName(sheetIndex, String.valueOf(i + 1)); } - wb.removeSheetAt(index); - return wb; + workbook.removeSheetAt(index); + return workbook; + } -// try { -// FileOutputStream out = new FileOutputStream(excelFile); -// out.flush(); -// sheets.write(out); -// out.close(); -// } catch (IOException e) { -// e.printStackTrace(); -// } + public static Workbook batchCloneSheet(File excelFile, Integer index, Integer total) throws IOException { + Workbook workbook = readExcelFromFile(excelFile); + return batchCloneSheet(workbook, index, total); } //读取excel public static Workbook readExcelFromFile(File file) throws IOException { if (file == null) { - return null; + throw new RuntimeException("文件为空"); + } + return readExcelFromInputStream(Files.newInputStream(file.toPath()), file.getName()); + } + + public static Workbook readExcelFromInputStream(InputStream inputStream, String fileName) throws IOException { + if (StrUtil.isBlank(fileName)) { + throw new RuntimeException("文件格式错误"); } - 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)); + return new HSSFWorkbook(inputStream); } else if ("xlsx".equals(suffix)) { - return new XSSFWorkbook(new FileInputStream(file)); + return new XSSFWorkbook(inputStream); } else { - throw new RuntimeException("文件格式错误,不是一个正经的Excel"); + throw new RuntimeException("文件格式错误"); } } + + public static String getStringCellValExcludeBlank(Cell cell) { + cell.setCellType(Cell.CELL_TYPE_STRING); + return cell.getStringCellValue().replaceAll("\\s*", ""); + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..505afbb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ - +server.port=18021 +spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/gmh?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai +spring.datasource.username=root +spring.datasource.password=root