Java 復雜excel報表導出


MyExcel,是一個可直接使用Html文件,或者使用內置的Freemarker、Groovy、Beetl等模板引擎Excel構建器生成的Html文件,以Html文件中的Table作為Excel模板來生成任意復雜布局的Excel的工具包,支持.xls、.xlsx格式,支持對背景色、邊框、字體等進行個性化設置,支持合並單元格。

Github:https://github.com/liaochong/myexcel

詳細文檔:https://github.com/liaochong/myexcel/wiki

maven引用:

<dependency>
    <groupId>com.github.liaochong</groupId>
    <artifactId>myexcel</artifactId>
    <version>2.1.1</version>
</dependency>

優點:

  • 可生成任意復雜表格:本工具使用迭代單元格方式進行excel繪制,可生成任意復雜度excel,自適應寬度、高度;
  • 零學習成本:使用html作為模板,學習成本幾乎為零;
  • 支持常用背景色、邊框、字體等樣式設置:具體參見文檔-Style-support(樣式支持)部分;
  • 支持.XLS、.XLSX:支持生成.xls、.xlsx后綴的excel;
  • 支持低內存SXSSF模式:支持低內存的SXSSF模式,可利用極低的內存生成.xlsx;
  • 支持生產者消費者模式導出:支持生產者消費者模式導出,配合SXSSF模式實現真正意義上海量數據導出;
  • 支持多種模板引擎:已內置Freemarker、Groovy、Beetl等常用模板引擎Excel構建器(詳情參見文檔Getting started),默認內置Beetl模板引擎(推薦引擎,Beetl文檔);
  • 提供默認Excel構建器,直接輸出簡單Excel:無需編寫任何html,已內置默認模板,可直接根據POJO數據列表輸出;
  • 支持一次生成多sheet:以table作為sheet單元,支持一份excel文檔中多sheet導出;

可選模板:

  1. 以下模板引擎除Beetl外默認均未被引入,使用者可根據自身需要選擇在pom.xml中聲明引入;
  2. 以下模板引擎版本為最低版本號;
<dependency>
    <groupId>com.ibeetl</groupId>
    <artifactId>beetl</artifactId>
    <version>2.7.23</version>
</dependency>

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-templates</artifactId>
    <version>2.4.13</version>
</dependency>

示例:

  1. 已存在html文件,使用這種方式時,html文件不局限於放在項目的classpath下
    // get html file
    File htmlFile = new File("/Users/liaochong/Downloads/example.html");
    // read the html file and use default excel style to create excel Workbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build();
    // this is a example,you can write the workbook to any valid outputstream OutputStream writer = new FileOutputStream(new File("/Users/liaochong/Downloads/excel.xlsx")); workbook.write(writer);
  2. 默認模板引擎使用
    List<Child> dataList = new ArrayList<>();
    for (int i = 0; i < 500000; i++) {
         Child child = new Child();
         child.setName("liaochong");
         child.setAge(i);
         child.setSex(1);
         child.setIndex(i);
         dataList.add(child);
    }
    Workbook workbook = DefaultExcelBuilder.getInstance().build(dataList);
    
    @ExcelTable(workbookType = WorkbookType.SXLSX, rowAccessWindowSize = 100, sheetName = "測試")
    public static class Child extends Parent {
         @ExcelColumn(order = 3, title = "姓名")
         private String name;
    
         @ExcludeColumn
         private Integer age;
    
         public String getName() {
             return name;
         }
    
         public void setName(String name) {
             this.name = name;
         }
    
         public Integer getAge() {
             return age;
         }
    
         public void setAge(Integer age) {
             this.age = age;
         }
    }
    
    public static class Parent {
    
         @ExcelColumn(title = "性別")
         private Integer sex;
    
         @ExcelColumn(order = -1, title = "index")
         private Integer index;
    
         public Integer getSex() {
              return sex;
         }
    
         public void setSex(Integer sex) {
              this.sex = sex;
         }
    
         public Integer getIndex() {
              return index;
         }
    
         public void setIndex(Integer index) {
              this.index = index;
         }
    }
  3. 使用freemarker等模板引擎,具體請參照項目中的example
    /**
         * use non-default-style excel builder
         *
         * @param response response
         */
        @GetMapping("/freemarker/build")
        public void build(HttpServletResponse response) {
            ExcelBuilder excelBuilder = new FreemarkerExcelBuilder();
            Workbook workbook = excelBuilder.template("/templates/freemarker_template.ftl").build(new HashMap<>());
    
            response.setCharacterEncoding(CharEncoding.UTF_8);
            response.addHeader("Content-Disposition", "attachment;filename=" + new String("freemarker_excel.xlsx".getBytes()));
            try {
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * use default-style excel builder
         *
         * @param response response
         */
        @GetMapping("/freemarker/default_style/build")
        public void buildWithDefaultStyle(HttpServletResponse response) {
            ExcelBuilder excelBuilder = new FreemarkerExcelBuilder();
            Workbook workbook = excelBuilder.template("/templates/freemarker_template.ftl").useDefaultStyle().build(new HashMap<>());
    
            response.setCharacterEncoding(CharEncoding.UTF_8);
            response.addHeader("Content-Disposition", "attachment;filename=" + new String("freemarker_excel.xlsx".getBytes()));
            try {
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

     


免責聲明!

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



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