jeecgboot–根据模板自定义导出
版本:jeecgboot2.4.3
jeecgboot框架自带的导入比较简洁,框架自带的关于导入导出的类是JeecgController,里面有导入导出的方法,但是自带的导出功能是根据实体类字段有加@Excel注解进行导出,我们在项目中可能会涉及到一些导出的个性化要求,例如还有统计功能,那就需要用模板导出来解决,具体如下:
1.后端
自定义导出业务类
/**
*
* @param id
* @return
* @throws Exception
*/
@AutoLog(value = "历史项目预算清单-excel导出")
@GetMapping(value = "/exportXsqd")
public ModelAndView exportXsqd(String id) throws Exception {
ProjectFilling filling = projectFillingService.getById(id);
//项目名称
String projecgName = filling.getProjecgName();
//送审金额
String chenkAmount = filling.getChenkAmount() == null ? ERROR : String.format("%,.2f", filling.getChenkAmount()); //送审金额
//核减金额
String cutAmount = filling.getCutAmount() == null ? ERROR : String.format("%,.2f", filling.getCutAmount());//核减金额
//审定金额
String definiteAmount = filling.getDefiniteAmount() == null ? ERROR : String.format("%,.2f", filling.getDefiniteAmount());//审定金额
//核减率
String rate = filling.getRateSum() == null ? "0.00%" : String.format("%.2f",(filling.getRateSum().multiply(new BigDecimal(100)).doubleValue())) + "%";
//获取项目清单列表
List<ProjectList> list = projectListService.getListByPid(id);
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> listMap = new ArrayList<>();
if (list.size() > 0) {
int i = 0;
for (ProjectList p : list) {
++i;
Map<String, Object> mp = new HashMap<>();
String cost = p.getCostName() == null ? ERROR : p.getCostName();//费用名称
String parameter = p.getParameter() == null ? ERROR : p.getParameter();//品牌与技术参数
String num = p.getQuantity() == null ? ERROR : p.getQuantity().toString();//数量
String unit = p.getUnit() == null ? ERROR : p.getUnit();//单位
String price = p.getPrice() == null ? ERROR : String.format("%,.2f", p.getPrice());//单价
//浮点数转换为string,并保留两位小数 String.format("%,.2f", p.getChenkAmount())
String chenk = p.getChenkAmount() == null ? ERROR :String.format("%,.2f", p.getChenkAmount());//送审金额
String cut = p.getCutAmount() == null ? ERROR : String.format("%,.2f", p.getCutAmount());//核减金额
String definite = p.getDefiniteAmount() == null ? ERROR : String.format("%,.2f", p.getDefiniteAmount());//审定金额
String cutRate = p.getCutRate() == null ? ERROR : String.format("%.2f",p.getCutRate()) + "%";//核减率
mp.put("no", i);//序号
mp.put("cost", cost);
mp.put("parameter", parameter);
mp.put("num", num);
mp.put("unit", unit);
mp.put("price", price);
mp.put("chenk", chenk);
mp.put("cut", cut);
mp.put("definite", definite);
mp.put("cutRate", cutRate);
//将项目清单的map添加到list中
listMap.add(mp);
}
}
//将项目清单集合添加到map中
map.put("listMap", listMap);
map.put("chenkAmount", chenkAmount);//项目清单总送审金额
map.put("cutAmount", cutAmount);//项目清单总核减金额
map.put("definiteAmount", definiteAmount);//项目清单总审定金额
map.put("rate", rate);//项目清单总核减率
map.put("projecgName", projecgName);//项目名称
File file = new File(ResourceUtils.getURL("classpath:").getPath());
//获取模板文件路径
File templateFile = new File(file, "/static/xls/预算清单报表.xls");
//获取模板
TemplateExportParams params = new TemplateExportParams(templateFile.getAbsolutePath()); //导入模板
ModelAndView mv = new ModelAndView(new JeecgTemplateExcelView()); //模板视图
//添加表格参数
mv.addObject(TemplateExcelConstants.PARAMS, params);
//添加模板参数
mv.addObject(TemplateExcelConstants.MAP_DATA, map);
return mv;
}
模板文件路径:
2.前端
导出方法
<a-button type="primary" icon="download" @click="handleExportXls()">导出</a-button>
methods: {
handleExportXls(fileName){
//导出文件名
if(!fileName || typeof fileName != "string"){
fileName = "预算清单报表"
}
let param = this.getQueryParams();
// if(this.selectedRowKeys && this.selectedRowKeys.length>0){
// param['selections'] = this.selectedRowKeys.join(",")
// }
let id = this.pid;
// alert(param.id)
downFile(this.url.exportXlsUrl,{"id":id}).then((data)=>{
if (!data) {
this.$message.warning("文件下载失败")
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
}else{
let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName+'.xls')
document.body.appendChild(link)
link.click()
document.body.removeChild(link); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放掉blob对象
}
})
},
}
3.小bug解决
问题:导出出现下面这个提示
原因:可能是制作模板的时候用了高版本的excel,出现了未兼容,本提示是由于我用了wps来制作模板(有提示但是文件是可以正常打开)
解决:通过office365来制作模板,导出未有提示