前段页面
var msg;
var loading = this.$loading({
lock: true,
text: msg,
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
axios({
url: basePath,
method: "post",
data: _this.queryParam,
responseType: "arraybuffer",
}).then(function (res) {
loading.close();
var blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'});
var fileNameSplit = res.headers['content-disposition'].split('=');
var fileName = decodeURI(fileNameSplit[fileNameSplit.length - 1])
if ('download' in document.createElement('a')) { // 非IE 下载
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
} else { // IE10+下载
navigator.msSaveOrOpenBlob(blob, fileName)
}
}).catch(function (reason) {
loading.close();
messageTip.error(whiteListTag, "导出操作失败...");
});
return false;
后台逻辑
在方法中获取参数:
@RequestBody Map<String,Object> param
设置响应
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String fileName = URLEncoder.encode("导出文件名称_" + date, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriter excelWriter = null;
try {
// 头的策略(背景设置为绿色)
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
WriteFont headWriteFont = new WriteFont();
// 字体大小
headWriteFont.setFontHeightInPoints((short)10);
headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略(这里用的是默认)
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short)10);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
// 这里 指定文件
excelWriter = EasyExcel.write(response.getOutputStream(), MyBean.class).registerWriteHandler(horizontalCellStyleStrategy).build();
autoWhiteListService.exportMethod(excelWriter,param);
} catch (Exception e) {
logger.error("导出Excel失败", e);
} finally {
// 关闭流
if (excelWriter != null) {
excelWriter.finish();
}
}
public void exportMethod(ExcelWriter excelWriter , Map<String, Object> queryParam) {
// 起始时间
logger.info("exportFeenoticeeWL 导出Excel开始Start" );
long start = System.currentTimeMillis();
// 查询信息
List<MyBean> MyBeanList= 查询结果;
WriteSheet writeSheet = EasyExcel.writerSheet(1, "Sheet1").build();
excelWriter.write(MyBeanList, writeSheet);
long exportTime = System.currentTimeMillis()-start;
logger.info("导出Excel总耗时: {}ms", exportTime);
}
@Getter
@Setter
public class MyBean{
@ColumnWidth(20)
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ColumnWidth(20)
@ExcelProperty(value = "性别", index = 1)
private String sex;
@ExcelIgnore
private String engName;
@ExcelProperty(value = "状态", index = 2)
private String state;
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>