依赖
<!--新版Excel 导入导出-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
实体类加注解
@Excel(name ="终端号",needMerge = true,width = 20,height = 8)
private String terminalCode;//终端号
单个sheet如何实现
/**
* 功能描述:默认导出方法
*
* @param list 导出的实体集合
* @param fileName 导出的文件名
* @param pojoClass pojo实体
* @param exportParams ExportParams封装实体
* @param response
* @return
*/
private static void defaultExport( List<?> list, Class<?> pojoClass, String fileName,
HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
多个sheet如何实现
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
ExportParams exportParams = new ExportParams("title", "sheetName");
map.put("title", exportParams);
map.put("entity", OrderReport.class);
map.put("data", “数据集”);
list.add(map);
/**
* 功能描述:默认导出方法
*
* @param list 导出的实体集合
* @param fileName 导出的文件名
* @param response pojo实体
* @param excelType 一般为ExcelType.HSSF
* @return
*/
private static void defaultSheetExport(List<Map<String, Object>> list, String fileName,
HttpServletResponse response, ExcelType excelType) {
Workbook workbook = ExcelExportUtil.exportExcel(list, excelType);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
最终导出
/**
* 功能描述:Excel导出
*
* @param fileName 文件名称
* @param response
* @param workbook Excel对象
* @return
*/
private static void downLoadExcel( String fileName, HttpServletResponse response,
Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
//定义标题以及设置响应头信息
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",URLEncoder.encode(fileName+".xls", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
vue下载文件
import fileDownload from ‘js-file-download’;
exportReport() {
this.$http({
url: ,
method: 'get',
responseType: 'arraybuffer'
}).then(response => {
let fileName = decodeURI(response.headers['content-disposition']);//-----获取响应头部里面filename的值
fileDownload(response.data, fileName); //-----根据fileDownload来下载响应回来的数据
});
},