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引用:

1<dependency> 2 <groupId>com.github.liaochong</groupId> 3 <artifactId>myexcel</artifactId> 4 <version>2.1.1</version> 5</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下

    1// get html file 2File htmlFile = new File("/Users/liaochong/Downloads/example.html"); 3// read the html file and use default excel style to create excel 4Workbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build(); 5// this is a example,you can write the workbook to any valid outputstream 6OutputStream writer = new FileOutputStream(new File("/Users/liaochong/Downloads/excel.xlsx")); 7workbook.write(writer);
  2. 默认模板引擎使用

    1List<Child> dataList = new ArrayList<>(); 2for (int i = 0; i < 500000; i++) { 3 Child child = new Child(); 4 child.setName("liaochong"); 5 child.setAge(i); 6 child.setSex(1); 7 child.setIndex(i); 8 dataList.add(child); 9} 10Workbook workbook = DefaultExcelBuilder.getInstance().build(dataList); 11 12@ExcelTable(workbookType = WorkbookType.SXLSX, rowAccessWindowSize = 100, sheetName = "测试") 13public static class Child extends Parent { 14 @ExcelColumn(order = 3, title = "姓名") 15 private String name; 16 17 @ExcludeColumn 18 private Integer age; 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public Integer getAge() { 29 return age; 30 } 31 32 public void setAge(Integer age) { 33 this.age = age; 34 } 35} 36 37public static class Parent { 38 39 @ExcelColumn(title = "性别") 40 private Integer sex; 41 42 @ExcelColumn(order = -1, title = "index") 43 private Integer index; 44 45 public Integer getSex() { 46 return sex; 47 } 48 49 public void setSex(Integer sex) { 50 this.sex = sex; 51 } 52 53 public Integer getIndex() { 54 return index; 55 } 56 57 public void setIndex(Integer index) { 58 this.index = index; 59 } 60}
  3. 使用freemarker等模板引擎,具体请参照项目中的example

    1/** 2 * use non-default-style excel builder 3 * 4 * @param response response 5 */ 6 @GetMapping("/freemarker/build") 7 public void build(HttpServletResponse response) { 8 ExcelBuilder excelBuilder = new FreemarkerExcelBuilder(); 9 Workbook workbook = excelBuilder.template("/templates/freemarker_template.ftl").build(new HashMap<>()); 10 11 response.setCharacterEncoding(CharEncoding.UTF_8); 12 response.addHeader("Content-Disposition", "attachment;filename=" + new String("freemarker_excel.xlsx".getBytes())); 13 try { 14 workbook.write(response.getOutputStream()); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 } 19 20 /** 21 * use default-style excel builder 22 * 23 * @param response response 24 */ 25 @GetMapping("/freemarker/default_style/build") 26 public void buildWithDefaultStyle(HttpServletResponse response) { 27 ExcelBuilder excelBuilder = new FreemarkerExcelBuilder(); 28 Workbook workbook = excelBuilder.template("/templates/freemarker_template.ftl").useDefaultStyle().build(new HashMap<>()); 29 30 response.setCharacterEncoding(CharEncoding.UTF_8); 31 response.addHeader("Content-Disposition", "attachment;filename=" + new String("freemarker_excel.xlsx".getBytes())); 32 try { 33 workbook.write(response.getOutputStream()); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )