使用:pom.xml引入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
具体实现
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) {
ServletOutputStream out = null;
BigExcelWriter writer = null;
try {
String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
writer = ExcelUtil.getBigWriter(file);
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
response.setCharacterEncoding("utf-8");
out = response.getOutputStream();
writer.flush(out, true);
} catch (Exception e){
e.printStackTrace();
}finally {
try{
if (out != null){
try {
IoUtil.close(out);
}catch (Exception e){
e.printStackTrace();
}
}
if (writer != null){
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}