Finish Task #9031 Cost:3h 开发导出word

This commit is contained in:
daijunxiong 2024-01-09 11:41:51 +08:00
parent 692b678d8e
commit 8bab53a026
2 changed files with 55 additions and 0 deletions

View File

@ -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<String, Object> 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();
}
}
}
}

View File

@ -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<String> excel(
@PathVariable String templateName,
@RequestBody Map<String, Object> 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);
}
}
/**
* 合同模板上传