Merge pull request 'dev' (#5) from dev into beta

Reviewed-on: crm/crm-print#5
This commit is contained in:
luobai 2024-01-12 09:13:24 +08:00
commit f170c60d3f
3 changed files with 134 additions and 1 deletions

View File

@ -9,6 +9,8 @@ import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -26,7 +28,7 @@ public class KkfileConfig {
*/ */
private static final long DEFAULT_TEMPLATE_MAX_SIZE = 10 * 1024 * 1024; private static final long DEFAULT_TEMPLATE_MAX_SIZE = 10 * 1024 * 1024;
private static final List<String> ALLOW_UPLOAD_TYPE = Collections.singletonList("docx"); private static final List<String> ALLOW_UPLOAD_TYPE =Arrays.asList("docx","xlsx");
/** /**
* 预览地址 * 预览地址

View File

@ -1,5 +1,7 @@
package cn.keking.service; 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.afterturn.easypoi.word.WordExportUtil;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.config.KkfileConfig; import cn.keking.config.KkfileConfig;
@ -7,10 +9,12 @@ import cn.keking.exception.KkFileException;
import cn.keking.service.impl.OtherFilePreviewImpl; import cn.keking.service.impl.OtherFilePreviewImpl;
import cn.keking.utils.Consts; import cn.keking.utils.Consts;
import cn.keking.utils.WebUtils; import cn.keking.utils.WebUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FilenameUtils; 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.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
@ -278,6 +282,37 @@ public class ContractService {
} }
} }
} }
public File downloadExcel(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("请先上传模板!");
}
String excelFile = "/"+ UUID.randomUUID().toString().replace("-", "") + ".xlsx";
//通过填参 生产文件 并放到 upload 文件夹中
String path = Consts.uploadAbsDir();
String pathFile = path + excelFile;
os = new FileOutputStream(pathFile);
TemplateExportParams params = new TemplateExportParams(
abTemplatePath);
Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap);
workbook.write(os);
return new File(pathFile);
} finally {
if (os != null) {
os.close();
}
}
}
@ -323,4 +358,39 @@ 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); 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);
}
}
/** /**
* 合同模板上传 * 合同模板上传
@ -111,6 +129,49 @@ public class ContractController {
} }
} }
/**
* @return 下载合同excel文件
*/
@ApiParam("下载excel文件")
@PostMapping("/downloadExcel/{templateName}")
public ServiceResponse downloadExcel(
@PathVariable String templateName,
@RequestBody Map<String, Object> dataMap,
HttpServletResponse response
) {
try {
log.error(JSON.toJSONString(dataMap));
OutputStream outstream = null;
File file = contractService.downloadExcel(templateName, dataMap);
outstream = response.getOutputStream();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String filename =file.getName();
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
// 刷新缓冲
response.flushBuffer();
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] b = new byte[bufferedInputStream.available()];
bufferedInputStream.read(b);
outstream.write(b);
bufferedInputStream.close();
outstream.flush();
outstream.close();
return new ServiceResponse<>("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, "fail", ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
/** /**
* @return 下载合同pdf文件 * @return 下载合同pdf文件
*/ */