easyExcel导出excel的简单使用

2019/03/18 11:53
阅读数 199

easyExcel导出excel的简单使用

         Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到KB级别,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便

 

easyexcel 项目git地址为: https://github.com/alibaba/easyexcel
官方使用指南见: https://github.com/alibaba/easyexcel/blob/master/quickstart.md

 

1:使用:

导入依赖maven依赖:

<dependency><groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beta4</version>
        </dependency>

 

2:模型映射

public class DownMonitorExcelSheet1 extends BaseRowModel {
    @ExcelProperty(value = "城市",index = 0)
    private String city;
    @ExcelProperty(value = "项目名字",index = 1)
    private String projectName;
    @ExcelProperty(value = "上刊数",index = 2)
    private Integer skNum;
    @ExcelProperty(value = "下刊数",index = 3)
    private Integer xkNum;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public Integer getSkNum() {
        return skNum;
    }

    public void setSkNum(Integer skNum) {
        this.skNum = skNum;
    }

    public Integer getXkNum() {
        return xkNum;
    }

    public void setXkNum(Integer xkNum) {
        this.xkNum = xkNum;
    }
}

 

3:service

 @Override
    public void excelExportDownInfo(String schemeId, String pushDate, HttpServletResponse response) throws IOException {
        String fileName = String.valueOf(System.currentTimeMillis());
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-Disposition", "filename=" + fileName+ ".xlsx");
        String sheet1Name = "下刊监测统计";
        String sheet2Name = "已下刊";
        String sheet3Name = "未下刊";
        ExcelWriter writer = new ExcelWriter(response.getOutputStream(), ExcelTypeEnum.XLSX);
        //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
        Sheet sheet = new Sheet(1, 0,DownMonitorExcelSheet1.class);
        sheet.setSheetName(sheet1Name);
        //下刊监测统计
        List<DownMonitorExcelSheet1> downMonitorExcelSheet1s = pushMonitorDao.queryDownMonitorExcelSheet1(schemeId, pushDate);
        writer.write(downMonitorExcelSheet1s, sheet);
//已下刊 sheet2 // List<DownMonitorExcelSheet2> downMonitorExcelSheet2s = pushMonitorDao.queryDownMonitorExcelSheet2(schemeId, pushDate); // Sheet sheet2 = new Sheet(2, 0, DownMonitorExcelSheet2.class); // sheet2.setSheetName(sheet2Name); // writer.write(downMonitorExcelSheet2s,sheet2); //未下刊 sheet3 // List<DownMonitorExcelSheet2> downMonitorExcelSheet3 = pushMonitorDao.queryDownMonitorExcelSheet3(schemeId, pushDate); // Sheet sheet3 = new Sheet(3, 0, DownMonitorExcelSheet2.class); // sheet3.setSheetName(sheet3Name); // writer.write(downMonitorExcelSheet3,sheet3); writer.finish(); }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部