easyExcel简介:
Java领域解析、生成Excel比较有名的框架有Apache poi、jxl等。但他们都存在一个严重的问题就是非常的耗内存。如果你的系统并发量不大的话可能还行,但是一旦并发上来后一定会OOM或者JVM频繁的full gc。
easyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。
easyExcel采用一行一行的解析模式,并将一行的解析结果以观察者的模式通知处理
easyExcel能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。
1.导入依赖【poi不能低于3.17,不然可能会报错】
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta5</version>
</dependency>
2.控制层
/**
* 导出学生缴费信息
* @param response
*/
@RequestMapping("/exportExcel")
public void easyDownload(HttpServletResponse response,String gradeBid){
//查询学生信息
List<ExportModel> list = importExcelService.getStudentExportList(gradeBid);
try {
OutputStream out = getOutputStream(response, "淮南二中学籍管理学生缴费信息", ExcelTypeEnum.XLSX);
//这里指定需要表头,因为model通常包含表信头息
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
//写第一个sheet,数据全是List<String> 无模型映射关系
Sheet sheet = new Sheet(1, 0,ExportModel.class);
//设置自适应宽度
sheet.setAutoWidth(Boolean.TRUE);
//设置表格样式
sheet.setTableStyle(createTableStyle());
//设置sheetName
sheet.setSheetName("淮南二中学籍管理学生缴费信息");
long start = System.currentTimeMillis() / 1000;//单位秒
//写数据
writer.write(list, sheet);
//关闭writer的输出流
writer.finish();
long end = System.currentTimeMillis() / 1000;
System.out.println("导出耗时:" + (end - start) +" 秒");
} catch (Exception e) {
}
}
/**
* 设置表格样式
* @return
*/
private TableStyle createTableStyle() {
TableStyle tableStyle = new TableStyle();
Font headFont = new Font();
headFont.setBold(true);
headFont.setFontHeightInPoints((short) 20);
headFont.setFontName("楷体");
tableStyle.setTableHeadFont(headFont);
tableStyle.setTableHeadBackGroundColor(IndexedColors.LIGHT_GREEN);
Font contentFont = new Font();
contentFont.setFontHeightInPoints((short) 12);
contentFont.setFontName("黑体");
tableStyle.setTableContentFont(contentFont);
return tableStyle;
}
/**
* 得到流
* @param response 响应
* @param fileName 文件名
* @param excelTypeEnum excel类型
* @return
*/
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) {
//LOGGER.error("EasyExcelUtil-->getOutputStream exception:", e);
}
return null;
}
3.导出模型
package com.iflytek.edu.hnezxjgl.model;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
@Data
public class ExportModel extends BaseRowModel{
/**
* 账号
*/
@ExcelProperty(value = {"账号"}, index = 0)
private String platformNum;
/**
* 姓名
*/
@ExcelProperty(value = {"姓名"}, index = 1)
private String name;
/**
* 身份证号
*/
@ExcelProperty(value = {"身份证号"}, index = 2)
private String idCardNum;
/**
* 性别
*/
@ExcelProperty(value = {"性别"}, index = 3)
private String sexName;
/**
* 年级
*/
@ExcelProperty(value = {"年级"}, index = 4)
private String gradeName;
/**
* 班级
*/
@ExcelProperty(value = {"班级"}, index = 5)
private String className;
/**
* 学费缴费状态名称
*/
@ExcelProperty(value = "学费缴费状态名称",index = 6)
private String studyFeeStatusName;
/**
* 书本费缴费状态名称
*/
@ExcelProperty(value = "书本费缴费状态名称",index = 7)
private String bookFeeStatusName;
}