zoukankan      html  css  js  c++  java
  • 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(); }
  • 相关阅读:
    【体验】在Adobe After Effects CC 2018中使用脚本创建窗口
    flask中错误使用flask.redirect('/path')导致的框架奇怪错误
    01-复杂度2 Maximum Subsequence Sum
    01-复杂度1 最大子列和问题
    02-线性结构1 两个有序链表序列的合并
    bfs—迷宫问题—poj3984
    bfs—Dungeon Master—poj2251
    bfs—Catch That Cow—poj3278
    GPTL—练习集—006树的遍历
    DB2存储过程——参数详解
  • 原文地址:https://www.cnblogs.com/dw3306/p/10551330.html
Copyright © 2011-2022 走看看