From e428e56fc850acc0cdf75a9d31abcd973501a021 Mon Sep 17 00:00:00 2001 From: chenda Date: Thu, 17 Nov 2022 22:01:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gmh/controller/ExcelController.java | 36 +++-- src/main/java/com/gmh/test.java | 129 ++++++++++++------ 2 files changed, 112 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/gmh/controller/ExcelController.java b/src/main/java/com/gmh/controller/ExcelController.java index 1fa8ca2..e4fe9a8 100644 --- a/src/main/java/com/gmh/controller/ExcelController.java +++ b/src/main/java/com/gmh/controller/ExcelController.java @@ -8,33 +8,27 @@ import com.gmh.entity.*; import com.gmh.entity.properties.FileServerProperties; import com.gmh.entity.query.ExecuteTemplateQuery; import com.gmh.entity.vo.ReadNameVo; -import com.gmh.service.GmhFileLogService; import com.gmh.service.FileService; +import com.gmh.service.GmhFileLogService; import com.gmh.service.SysBaijiaxingService; -import com.gmh.utils.StringUtils; import com.gmh.utils.POIExcelUtil; +import com.gmh.utils.StringUtils; import lombok.AllArgsConstructor; import lombok.Getter; -import org.apache.commons.fileupload.disk.DiskFileItem; -import org.apache.commons.fileupload.disk.DiskFileItemFactory; -import org.apache.commons.io.IOUtils; 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.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.commons.CommonsMultipartFile; -import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.util.*; import java.util.stream.Collectors; @@ -54,13 +48,20 @@ public class ExcelController { @Autowired private SysBaijiaxingService baijiaxingService; - private final ExcelInfo excelInfo = new ExcelInfo(); + @Autowired + private HttpServletRequest request; + + /** + * HashMap的Key为SessionId + * + * 通过SessionId为每一个会话保存自己的对象副本 + * + * 不适用于集群环境 + */ + private final Map excelInfoMap = new HashMap<>(); private static final String KEY_XINGMING = "姓名"; - @Value("${gmh.upload-path}") - private String uploadPath; - @RequestMapping("/t01") public R test01() { return R.ok("ok"); @@ -68,6 +69,7 @@ public class ExcelController { @RequestMapping("/dataSourceImport") public R dataSourceImport(MultipartFile file, String dataSourceImportModel) throws IOException { + excelInfoMap.put(request.getSession().getId(), new ExcelInfo()); DataSourceImportModel importModel = DataSourceImportModel.findEnum(dataSourceImportModel); Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); FileEntity fileEntity = fileService.uploadFile(file); @@ -113,6 +115,7 @@ public class ExcelController { readNameVo.setSheetName(sheet.getSheetName()); readNameVoList.add(readNameVo); } + ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); excelInfo.setReadNameVoList(readNameVoList); return R.ok().setData(readNameVoList); } @@ -155,6 +158,7 @@ public class ExcelController { Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); FileEntity fileEntity = fileService.uploadFile(file); gmhFileLogService.recordLog(fileEntity, GmhFileLog.OperateType.TEMPL_IMP); + ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); excelInfo.setTemplateOriginalFilename(file.getOriginalFilename()); excelInfo.setTemplateSheetNum(Integer.parseInt(templateSheetNum) - 1); Sheet sheet = workbook.getSheetAt(excelInfo.getTemplateSheetNum()); @@ -167,6 +171,7 @@ public class ExcelController { @RequestMapping("/cellNumSearch") public R analysisCell(@Validated RowCellNum rowCellNum) { + ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); if (excelInfo.getTemplateWorkbook() == null) { return R.error("模板文件出问题了,请重新上传"); } @@ -190,6 +195,7 @@ public class ExcelController { @PostMapping("/executeTemplate") public R executeTemplate(@RequestBody ExecuteTemplateQuery executeTemplateQuery) throws IOException { + // TODO 有表达式和无表达式兼容处理 String newCellData = executeTemplateQuery.getNewCellData(); String expr = newCellData.substring(newCellData.indexOf("@{"), newCellData.indexOf("}") + 1); String exprVal = newCellData.substring(newCellData.indexOf("@{") + 2, newCellData.indexOf("}")); @@ -197,6 +203,7 @@ public class ExcelController { String sheetName = exprArr[0]; String field = exprArr[1]; + ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); Optional first = excelInfo.getReadNameVoList() .stream() .filter(item -> item.getSheetName().equalsIgnoreCase(sheetName)) @@ -269,10 +276,13 @@ public class ExcelController { @GetMapping("/gotoDownload") public R gotoDownload() { + ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); try { excelInfo.getTemplateWorkbook().close(); } catch (IOException e) { throw new RuntimeException(e); + } finally { + excelInfoMap.remove(request.getSession().getId()); } return R.ok(); } diff --git a/src/main/java/com/gmh/test.java b/src/main/java/com/gmh/test.java index 6c531cf..e288cd1 100644 --- a/src/main/java/com/gmh/test.java +++ b/src/main/java/com/gmh/test.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -23,27 +24,47 @@ import java.util.List; 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)); - } +// 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)); +// } + + String bz = "牟春华\n" + + "陈文硕\n" + + "邱晓\n" + + "李英\n" + + "王贺\n" + + "邱萌\n" + + "李世祺\n" + + "王槐秀\n" + + "芦健平\n" + + "刘志佳\n" + + "陈东移\n" + + "谭俊英\n" + + "杨绍华\n" + + "孙洪艳\n" + + "张庆华\n" + + "白淼\n" + + "吴铭涛\n"; + + List bianzhiList = Arrays.asList(bz.split("\n")); 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++) { @@ -60,7 +81,7 @@ public class test { } try { - FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明222.xls"); + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明1112.xls"); out.flush(); bianzhiWb.write(out); out.close(); @@ -71,23 +92,51 @@ public class test { // 聘任制 - 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); - } +// 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); +// } + String prz = "梁雪\n" + + "娄万明\n" + + "刘俊苹\n" + + "刘洋洋\n" + + "武广瑞\n" + + "单春飞\n" + + "罗酩禹\n" + + "王旭\n" + + "王梓\n" + + "侯佳君\n" + + "管静\n" + + "林源\n" + + "庄稀月\n" + + "宋媛媛\n" + + "苏爽\n" + + "张萌\n" + + "张霖琳\n" + + "余金萍\n" + + "孙佳馨\n" + + "石子宁\n" + + "苏欣\n" + + "李仕伟\n" + + "刘畅\n" + + "李昱霖\n" + + "闫文凝\n"; + + List pinrenList = Arrays.asList(prz.split("\n")); + 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); @@ -103,7 +152,7 @@ public class test { } try { - FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制)222.xls"); + FileOutputStream out = new FileOutputStream("C:\\Users\\xxche\\Desktop\\20221106_test_excel\\附件3.继续教育证明(聘用制)1112.xls"); out.flush(); pinrenWb.write(out); out.close();