easyExcel實現Excel導出功能


一、簡介

    作者對easyExcel的描述: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模式。在上層做了模型轉換的封裝,讓使用者更加簡單方便。

二、快速上手

使用步驟:

  1. 添加依賴
    1   <!--Excel包-->
    2         <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
    3         <dependency>
    4             <groupId>com.alibaba</groupId>
    5             <artifactId>easyexcel</artifactId>
    6             <version>1.1.2-beta5</version>
    7         </dependency>

     

      1. 七行代碼就可以實現簡單的Excel導出 
        @Test
            public void  ExportTest() throws IOException {
                //指定文件輸出位置
                OutputStream outputStream =new FileOutputStream("H:/excel/file/myexcel3.xlsx");
                ExcelWriter excelWriter =EasyExcelFactory.getWriter(outputStream);
                //將要輸出的內容填充到Sheet里
                Sheet sheet =new Sheet(1,0,ExcelModel.class );
                //設置sheet表名
                sheet.setSheetName("my_three_excel");
                /**
                 * 寫數據到Write上下文中
                 * 第一個參數:要寫入的內容
                 * 第二個參數:要寫入的sheet目標
                 */
                excelWriter.write(createModelList2(),sheet);
                excelWriter.finish();
                outputStream.close();
            }

         

      2. 上面使用到的ExcelModel.class需要自己定義,首先需要繼承BaseRowModel,其次,通過 @ExcelProperty 注解來指定每個字段的列名稱,以及下標位置;

        @Data
        public class ExcelMode extends BaseRowModel {
            @ExcelProperty(value = "姓名" ,index = 0)
            private  String  userName;
            @ExcelProperty(value = "年齡" ,index = 1)
            private String age;
            @ExcelProperty(value = "住址" ,index = 2)
            private String address;
        }

        填充的數據:

        private List<ExcelMode> createModelList (){
                List<ExcelMode> list = new ArrayList<>();
                for(int i=0; i<20;i++){
                    ExcelMode excelMode = new ExcelMode();
                    excelMode.setUserName("噠噠"+i);
                    excelMode.setAge("22");
                    excelMode.setAddress("廣西");
                    list.add(excelMode);
                }
                return list;
            }

      3.  

        設置二級表頭

        @Data
        public class ExcelModel2 extends BaseRowModel {
            @ExcelProperty(value = {"name","name"},index = 0)
            private  String name;
            @ExcelProperty(value ={"age","age"},index = 1)
            private  String age;
            @ExcelProperty(value={"cash_value","高"},index = 2)
            private String cashvalue_high ;
            @ExcelProperty(value={"cash_value","中"},index = 3)
            private String cashvalue_during ;
            @ExcelProperty(value={"cash_value","低"},index = 4)
            private String cashvalue_low ;
        
        }
        @Data
        public class ExcelModel2 extends BaseRowModel {
            @ExcelProperty(value = {"name","name"},index = 0)
            private  String name;
            @ExcelProperty(value ={"age","age"},index = 1)
            private  String age;
            @ExcelProperty(value={"cash_value","高"},index = 2)
            private String cashvalue_high ;
            @ExcelProperty(value={"cash_value","中"},index = 3)
            private String cashvalue_during ;
            @ExcelProperty(value={"cash_value","低"},index = 4)
            private String cashvalue_low ;
        
        }

        結果:

         


免責聲明!

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



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