后端接口
@GetMapping("/exportTrack")
public void exportTrack(HttpServletResponse response) throws IOException {
OutputStream out = null;
try {
out = getOutputStream(response,"用户信息表",ExcelTypeEnum.XLSX);
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
List<Map<String, Object>> list = cardlistService.selectAllCardList();
List<CardlistExcelVO> tdevicedtlVos = BeanUtils.mapsToBeans(list, CardlistExcelVO.class);
List<List<CardlistExcelVO>> partition = Lists.partition(tdevicedtlVos, 2000);
for (int i = 0; i < partition.size(); i++) {
com.alibaba.excel.metadata.Sheet sheet = new Sheet((i + 1), 0, CardlistExcelVO.class, "sheet" + (i + 1), null);
writer.write(partition.get(i), sheet);
}
writer.finish();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private OutputStream getOutputStream(HttpServletResponse response, String fileName,
ExcelTypeEnum excelTypeEnum) {
try {
// 设置响应输出的头类型
if (Objects.equals(".xls", excelTypeEnum.getValue())) {
//导出xls格式
response.setContentType("application/vnd.ms-excel;charset=GBK");
} else if (Objects.equals(".xlsx", excelTypeEnum.getValue())) {
//导出xlsx格式
response.setContentType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=GBK");
}
// 设置下载文件名称(注意中文乱码)
response.addHeader("Content-Disposition",
"attachment;filename=" + new String((fileName).getBytes("GB2312"), "ISO8859-1") + excelTypeEnum
.getValue());
response.addHeader("Pragma", "No-cache");
response.addHeader("Cache-Control", "No-cache");
response.setCharacterEncoding("utf8");
return response.getOutputStream();
} catch (IOException e) {
log.error("EasyExcelUtil-->getOutputStream exception:", e);
}
return null;
}
前端
(1)前端接口必须返回类型为responseType:‘blob’
export const exportTrack= (reqParams) => request({
url: baseOtherUrl + '/SpecialAaccountBastionHost/exportBastionHost',
method: 'post',
responseType: 'blob',
data: reqParams
});
(2)vue使用blob
exportBehaviorTrack: function () {
exportTrack({key:value}).then(response => {
if (window.navigator.msSaveOrOpenBlob) {
// 兼容IE
const blob = new Blob([response]);
window.navigator.msSaveBlob(blob, "行为轨迹信息表.xls");
} else { // 处理返回的文件流
const blob = response.data
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = '行为轨迹信息表.xls'
document.body.appendChild(link)
link.click()
window.setTimeout(function () {
URL.revokeObjectURL(blob)
document.body.removeChild(link)
}, 0)
}
})
},
Java mapper
1、 List<Map<String,Object>> selectAllCardList();
2、<select id="selectAllCardList" resultType="java.util.HashMap">
select * from t_cardlist
</select>