Finish Task #9031 Cost:3h 开发导出word
This commit is contained in:
parent
aefd70308e
commit
ac1274b565
@ -9,6 +9,7 @@ import cn.keking.exception.KkFileException;
|
||||
import cn.keking.service.impl.OtherFilePreviewImpl;
|
||||
import cn.keking.utils.Consts;
|
||||
import cn.keking.utils.WebUtils;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
@ -281,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -360,4 +392,5 @@ public class ContractService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -129,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文件
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user