自定义模板导出(一)问题是在本地环境可以导出部署到服务器(docker容器)中,会找不到导出模板地址和下载路径地址
时隔一年,终于在同事们的帮助下解决了
将模板映射到容器
后端
dev1与prod1为外部服务器
后端读取模板路径代码与模板写出
@Value("${activeFile}")
private String activeFile;
String a = null;
if ("test1".equals(activeFile)){
a = new ClassPathResource("static/poi/badL.xlsx").getAbsolutePath();
}else if ("dev1".equals(activeFile)){
a = "/jb_saijia/badL.xlsx";
}else if ("prod1".equals(activeFile)){
a = "/jbsj/badL.xlsx";
}
TemplateExportParams params = new TemplateExportParams(a);
map.put("maplist", listMap);
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
String fileName = URLEncoder.encode("不合格品维修记录.xlsx", "utf-8");
OutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
workbook.write(out);
out.flush();
out.close();
前端
exportXls(record) {
this.queryParam.id = record.id;
let param = this.getQueryParams();
console.log("导出参数", param)
downFile(this.url.exportXlsUrl, param).then((res) => {
console.log(res)
const filename = "不合格品维修记录"
const url = window.URL.createObjectURL(new Blob([res]))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', filename+".xlsx")
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectRUL(url)
}).catch(err=>{
// console.log(err.response.data.message)
})
},