easyExcel導出excel的簡單使用


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);
// 設置表格樣式
TableStyle tableStyle = new TableStyle();
tableStyle.setTableHeadBackGroundColor(IndexedColors.SKY_BLUE);
tableStyle.setTableContentBackGroundColor(IndexedColors.WHITE1);
Font font = new Font();
font.setBold(false);
font.setFontName("微軟雅黑");
font.setFontHeightInPoints((short) 9);
tableStyle.setTableContentFont(font);
sheet.setTableStyle(tableStyle);
//已下刊 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(); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM