后端部分:
1、导出依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.11</version>
</dependency>
2、controller层代码
@RequestMapping("/exportExcel")
@ResponseBody
public void exportExcel(@RequestBody CsLawsuitOrderDTO dto, HttpServletResponse response) {
ServerValidUtils.validObj(dto, "参数不允许为空");
// 查询所有数据,最多下载2000条
dto.setPageSize(2000);
IPage<CsLawsuitOrder> page = csLawsuitOrderService.queryByPage(dto);
ServerValidUtils.valid(page == null || page.getTotalPageCount() == 0, "没有数据,请重新选择筛选条件");
ServerValidUtils.valid(page.getTotalPageCount() > 2000, "数据超过2000条,请重新选择筛选条件");
List<CsLawsuitOrder> data = (List<CsLawsuitOrder>) page.getData();
try {
response.reset();
String fileName = URLEncoder.encode("诉讼列表".concat(DateTimeUtil.format(new Date(),
DateTimeUtil.pattern_ymd_hms)).concat(ExcelTypeEnum.XLSX.getValue()),
"UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setContentType("application/msexcel");
response.setCharacterEncoding("ISO-8859-1");
try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
EasyExcel.write(response.getOutputStream(), CsLawsuitOrder.class).sheet("诉讼列表").doWrite(data);
bos.flush();
}
} catch (Exception e) {
logger.error("下载文件出现异常,异常信息为", e);
}
}
前端接收:
//导出Excel数据
exportCur() {
// 简易版
// window.open(
// `${location.origin}${
// process.env.VUE_APP_BASE_API
// }/lawsuitOrder/exportExcel?${qs.stringify({ ...this.form })}`
// );
// window.location = `${location.origin}${
// process.env.VUE_APP_BASE_API
// }/lawsuitOrder/exportExcel?${qs.stringify({ ...this.form })}`;
// axios接收版
if (this.btnList[2].title === "导出excel中...") {
this.$modal.msg("导出excel中,请勿重复点击!");
return;
}
this.btnList[2].title = "导出excel中...";
this.$store
.dispatch("lawsuitBatchList/ExportExcel", { ...this.form })
.then((data) => {
// let blob = new Blob([data]);
const blobUrl = window.URL.createObjectURL(data);
const link = document.createElement("a");
document.body.appendChild(link);
link.style.display = "none";
link.href = blobUrl;
link.download = `诉讼列表${dayjs(Date.now()).format(
"YYYY-MM-DD HH:mm:ss"
)}.xlsx`;
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(blobUrl);
this.btnList[2].title = "待诉导出";
})
.catch(() => {
this.btnList[2].title = "待诉导出";
});
},
// 导出全部excel
export function exportExcel(options) {
return axios({
method: 'post',
url: baseURL+`${process.env.VUE_APP_OLD_APP}/lawsuitOrder/exportExcel`,
responseType: 'blob', //设置返回信息为二进制文件,默认为json
data: JSON.stringify(options) // //后台照常用@RequestBody接收即可
})
}
ps:前端dayjs工具包函数需要自己导入packge.json里面加一个 "dayjs": "^1.10.7" 的依赖; import dayjs from "dayjs"