Html2excel,是一个可直接使用Html文件,或者使用内置的Freemarker、Groovy、Beetl等模板引擎Excel构建器生成的Html文件,以Html文件中的Table作为Excel模板来生成任意复杂布局的Excel的工具包,支持.xls、.xlsx格式,支持对背景色、边框、字体等进行个性化设置,支持合并单元格。
优点|Advantages可生成任意复杂表格:本工具使用迭代单元格方式进行excel绘制,可生成任意复杂度excel,自适应宽度、高度;
零学习成本:使用html作为模板,学习成本几乎为零;
支持常用背景色、边框、字体等样式设置:具体参见文档-Style-support(样式支持)部分;
支持.XLS、.XLSX:支持生成.xls、.xlsx后缀的excel;
支持低内存SXSSF模式:支持低内存的SXSSF模式,可利用极低的内存生成.xlsx;
支持生产者消费者模式导出:支持生产者消费者模式导出,配合SXSSF模式实现真正意义上海量数据导出;
支持多种模板引擎:已内置Freemarker、Groovy、Beetl等常用模板引擎Excel构建器(详情参见文档Gettingstarted),默认内置Beetl模板引擎(推荐引擎,Beetl文档);
提供默认Excel构建器,直接输出简单Excel:无需编写任何html,已内置默认模板,可直接根据POJO数据列表输出;
支持一次生成多sheet:以table作为sheet单元,支持一份excel文档中多sheet导出;
其他诸如API接口、异常说明、常见问题(性能等)等请参见具体文档:https://github.com/liaochong/html2excel/wiki示例|Example已存在html文件,使用这种方式时,html文件不局限于放在项目的classpath下
// get html fileFile htmlFile = new File("/Users/liaochong/Downloads/example.html");// read the html file and use default excel style to create excelWorkbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build();// this is a example,you can write the workbook to any valid outputstreamOutputStream writer = new FileOutputStream(new File("/Users/liaochong/Downloads/excel.xlsx"));workbook.write(writer);
使用freemarker等模板引擎,具体请参照项目中的example
/*** 使用普通方式构建excel** @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(); }}/*** 使用默认样式构建excel** @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(); }}
默认模板引擎使用
注解方式
ListdataList = 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; }}
自定义方式
// titleListtitles = new ArrayList<>();titles.add("姓名");titles.add("年龄");// field display orderListorder = new ArrayList<>();order.add("name");order.add("age");// display dataListdataList = this.getData();Workbook workbook = DefaultExcelBuilder.getInstance() .sheetName("default example") .titles(titles) .fieldDisplayOrder(order) .build(dataList);private ListgetData(){ TestDO testDO = new TestDO(); testDO.setName("张三"); TestDO testDO1 = new TestDO(); testDO1.setName("李四"); TestDO testDO2 = new TestDO(); testDO2.setName("王五"); testDO2.setAge(15); TestDO testDO3 = new TestDO(); testDO3.setName("陈六"); testDO3.setAge(25); ListdataList = new ArrayList<>(); dataList.add(testDO); dataList.add(testDO1); dataList.add(testDO2); dataList.add(testDO3); return dataList;}
评论