diff --git a/server/src/main/java/cn/keking/service/ContractService.java b/server/src/main/java/cn/keking/service/ContractService.java index 5b39011..d147b34 100644 --- a/server/src/main/java/cn/keking/service/ContractService.java +++ b/server/src/main/java/cn/keking/service/ContractService.java @@ -1,5 +1,7 @@ package cn.keking.service; +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import cn.afterturn.easypoi.word.WordExportUtil; import cn.keking.config.ConfigConstants; import cn.keking.config.KkfileConfig; @@ -11,6 +13,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections4.MapUtils; import org.apache.commons.io.FilenameUtils; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge; @@ -323,4 +326,38 @@ public class ContractService { } } } + + public String excel(String templateName, Map dataMap) throws Exception { + OutputStream os = null; + try { + File outFile = new File(Consts.uploadAbsDir()); + if (!outFile.exists()) { + outFile.mkdirs(); + } + String templatePath = Consts.getTemplatePath(); + String abTemplatePath = templatePath + File.separator + templateName + ".Xlsx"; + //判断模板是否存在 + File file = new File(abTemplatePath); + if (!file.exists()) { + throw new KkFileException("请先上传模板!"); + } + TemplateExportParams params = new TemplateExportParams( + abTemplatePath); + Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap); + String docxFile = "/"+ UUID.randomUUID().toString().replace("-", "") + ".Xlsx"; + //通过填参 生产文件 并放到 upload 文件夹中 + String path = Consts.uploadAbsDir(); + String pathFile = path + docxFile; + os = new FileOutputStream(pathFile); + workbook.write(os); + log.info("文件路径:{}", kkfileConfig.getLocalfileUrl() + File.separator + Consts.UPLOAD_PATH_NAME + docxFile); + String previewUrl = WebUtils.getBaseUrl() + "onlinePreview?url=" + + URLEncoder.encode(Base64.encodeBase64String((kkfileConfig.getLocalfileUrl() + File.separator + Consts.UPLOAD_PATH_NAME + docxFile).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8.name()); + return previewUrl; + } finally { + if (os != null) { + os.close(); + } + } + } } diff --git a/server/src/main/java/cn/keking/web/controller/ContractController.java b/server/src/main/java/cn/keking/web/controller/ContractController.java index 22e9048..7db9299 100644 --- a/server/src/main/java/cn/keking/web/controller/ContractController.java +++ b/server/src/main/java/cn/keking/web/controller/ContractController.java @@ -61,6 +61,24 @@ public class ContractController { return new ServiceResponse<>(null, e.getMessage(), ServiceResponse.RESULT_SERVER_ERR_CODE); } } + @ApiOperation("根据模板渲染excel") + @PostMapping("/excel/{templateName}") + public ServiceResponse excel( + @PathVariable String templateName, + @RequestBody Map dataMap) { + try { + log.info("templateName:{},dataMap:{}", templateName, dataMap); + return new ServiceResponse<>(contractService.excel(templateName, dataMap), "success", + ServiceResponse.RESULT_CODE_SUCCESS_CODE); + } catch (Exception e) { + log.error("生成错误:", e); + if (e instanceof KkFileException) { + KkFileException kkFileException = (KkFileException) e; + return new ServiceResponse<>(null, kkFileException.getMessage(), ServiceResponse.RESULT_CODE_ERROR_CODE); + } + return new ServiceResponse<>(null, e.getMessage(), ServiceResponse.RESULT_SERVER_ERR_CODE); + } + } /** * 合同模板上传