Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出。
阿里开源,github:https://github.com/alibaba/easyexcel
com.alibaba
easyexcel
1.1.2-beta5
org.apache.poi
poi
4.0.0
org.apache.poi
poi-ooxml
4.0.0
cglib
cglib
3.3.0
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelEmployee extends BaseRowModel{
@ExcelProperty(value = {"员工信息","序号"} ,index = 0)
private String sortNo;
@ExcelProperty(value = {"员工信息","员工代码","EMPCODE"},index = 1)
private String employeeCode;
@ExcelProperty(value = {"员工信息","用户登录名","USERNAME"},index = 2)
private String userName;
@ExcelProperty(value = {"员工信息","员工姓名","EMPNAME"},index = 3)
private String trueName;
。。。。。。。。。。。。。
}
@Component
public class ExcelUtil {
private static StyleExcelHandler styleExcelHandler;
@Autowired
public void setStyleExcelHandler(StyleExcelHandler styleExcelHandler){
ExcelUtil.styleExcelHandler = styleExcelHandler;
}
/**
* @param inputStream 输入流
* @param sheetNo 工作表编号
* @param headLineMun 标题占用行数
*/
public static List
@Component
public class StyleExcelHandler implements WriteHandler {
@Override
public void sheet(int i, Sheet sheet) {
}
@Override
public void row(int i, Row row) {
}
@Override
public void cell(int i, Cell cell) {
Sheet sheet = cell.getSheet();
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = createStyle(workbook);
Font font = workbook.createFont();
font.setFontName("黑体");
String sheetName = sheet.getSheetName();
if(sheetName.equals("员工信息")) {
if(cell.getRowIndex() <= 2) {
if (i == 5) {
cellStyle.setFillPattern(FillPatternType.LESS_DOTS);//设置前景填充样式
cellStyle.setFillBackgroundColor((short)13);
cellStyle.setFillForegroundColor((short)13);
}
font.setFontHeightInPoints((short) 12);
}else{
font.setFontHeightInPoints((short) 8);
}
}else {
if(cell.getRowIndex() < 1) {
font.setFontHeightInPoints((short) 12);
}else {
font.setFontHeightInPoints((short) 8);
}
}
cellStyle.setFont(font);
cell.getRow().getCell(i).setCellStyle(cellStyle);
}
/**
* 实际中如果直接获取原单元格的样式进行修改, 最后发现是改了整行的样式, 因此这里是新建一个样* 式
*/
private CellStyle createStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
// 下边框
cellStyle.setBorderBottom(BorderStyle.THIN);
// 左边框
cellStyle.setBorderLeft(BorderStyle.THIN);
// 上边框
cellStyle.setBorderTop(BorderStyle.THIN);
// 右边框
cellStyle.setBorderRight(BorderStyle.THIN);
// 水平对齐方式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 垂直对齐方式
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
}
public static void main(String[] args) throws FileNotFoundException {
List excelParameterList = new ArrayList<>();
ExcelParameter excelParam1 = new ExcelParameter();
excelParam1.setSheetNo(1);
excelParam1.setHeadLineMun(2);
excelParam1.setSheetName("员工信息");
excelParam1.setStartRow(0);
MergeParameter m1 = new MergeParameter(1,2,0,0);
List mergeParameterList1 = new ArrayList<>();
mergeParameterList1.add(m1);
excelParam1.setMergeParameterList(mergeParameterList1);
Map columnWidth = new HashMap<>();
columnWidth.put(0, 2000);
columnWidth.put(1, 3000);
columnWidth.put(2, 4000);
columnWidth.put(3, 3000);
excelParam1.setColumnWidth(columnWidth);
excelParameterList.add(excelParam1);
OutputStream outputStream = new FileOutputStream(new File("E:\\test.xlsx"));
List> employeeDate = new ArrayList<>();
employeeDate.add(Arrays.asList(new ExcelEmployee()));
writeExcel(outputStream, excelParameterList, employeeDate);
}