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导出;
可选模板:
- 以下模板引擎除Beetl外默认均未被引入,使用者可根据自身需要选择在pom.xml中声明引入;
- 以下模板引擎版本为最低版本号;
<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>
示例:
- 已存在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); - 默认模板引擎使用
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; } }
- 使用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(); } }