增加配置文件

集成MyBatis-Plus
增加文件导入接口
完成名单解析的部分功能
master
lenovo 2 years ago
parent 1c4f91635b
commit 01fb76f5d7

@ -62,6 +62,14 @@
<artifactId>hutool-all</artifactId>
<version>5.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
<build>

@ -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<String, List<GmhUser>> sheetNameList = new LinkedHashMap<>();
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
List<GmhUser> 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();
}
}

@ -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<GmhUser> list = gmhUserService.list();
return R.ok("success").setData(list);
}
}

@ -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<com.gmh.entity.GmhUser> {
}

@ -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<SysBaijiaxing> {
}

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

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

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

@ -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<GmhUser> {
}

@ -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<SysBaijiaxing> {
}

@ -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<GmhUserDao, GmhUser> implements GmhUserService {
}

@ -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<SysBaijiaxingDao, SysBaijiaxing> implements SysBaijiaxingService {
}

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

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

@ -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));
}
workbook.removeSheetAt(index);
return workbook;
}
wb.removeSheetAt(index);
return wb;
// 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*", "");
}
}

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

Loading…
Cancel
Save