java
/**
* 导出数据
*/
@GetMapping("exportData")
public void exportData(FixedAssetsEntityPoJo fixedAssets, HttpServletResponse response) throws IOException {
IPage<FixedAssetsEntity> fixedAssetsEntityList = fixedAssetsService.getFixedAssets(new Page<>(1, Integer.MIN_VALUE), fixedAssets);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("IT资产管理导出数据表", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
EasyExcel.write(byteArrayOutputStream, FixedAssetsEntity.class).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("IT固定资产").doWrite(fixedAssetsEntityList.getRecords());
response.setContentLengthLong(byteArrayOutputStream.size());
OutputStream outputStream= response.getOutputStream();
EasyExcel.write(outputStream, FixedAssetsEntity.class).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("IT固定资产").doWrite(fixedAssetsEntityList.getRecords());
}
js
this.$http({
url: this.$http.adornUrl("/fixedAssets/exportData"),
method: "get",
responseType: "blob",
params: this.$http.adornParams(this.ajaxSeachData),
timeout:1000 * 30000,
decompress:true,
onDownloadProgress: (evt) => {
this.percentage = parseInt(
(evt.loaded / evt.total) * 100
);
if(this.percentage==100){
this.progressSuccess='success';
}
},
}).then(({ data }) => {
if (data.code == 500) {
this.$message.error(data.msg);
} else {
const blob = new Blob([data], {
type: "application/octet-stream; charset=utf-8",
});
//对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
//IE10以上支持blob但是依然不支持download
if ("download" in document.createElement("a")) {
//支持a标签download的浏览器
var fileName="IT固定资产"+this.getNowTimeStr()+".xls";
const link = document.createElement("a"); //创建a标签
link.download = fileName; //a标签添加属性
link.style.display = "none";
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click(); //执行下载
URL.revokeObjectURL(link.href); //释放url
document.body.removeChild(link); //释放标签
} else {
//其他浏览器
navigator.msSaveBlob(blob, fileName);
}
}
this.showDownload = false;
this.downLoadDialogVisible=false;
});
网友评论