html2excel, 是转换 HTML 表格为 Excel 的工具包, 支持. xls,.xlsx 格式, 支持对背景色, 边框, 字体等进行个性化设置.
GitHub: https://github.com/liaochong/html2excel
maven 引用:
- <dependency>
- <groupId>com.GitHub.liaochong</groupId>
- <artifactId>html2excel</artifactId>
- <version>1.2.0</version>
- </dependency>
优点:
使用 HTML 作为模板, 学习成本几乎为零;
可生成任意复杂表格: 本工具使用迭代单元格方式进行 Excel 绘制, 可生成任意复杂度 Excel;
屏蔽 POI 操作: 不直接操作过重的 POI;
支持常用背景色, 边框, 字体等样式设置: 具体参见详情部分;
支持. XLS,.XLSX: 支持生成. xls,.xlsx 后缀的 Excel;
支持多种模板引擎: 支持 Freemarker,Groovy,Beetl 等常用模板引擎, 默认内置 Beetl 模板引擎 (推荐引擎, Beetl 文档 http://ibeetl.com/guide/#beetl );
提供默认模板引擎, 直接输出简单 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);
默认模板引擎使用 (暂不支持继承属性)
- // title
- List<String> titles = new ArrayList<>();
- titles.add("姓名");
- titles.add("年龄");
- // field display order
- List<String> order = new ArrayList<>();
- order.add("name");
- order.add("age");
- // display data
- 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);
- List<TestDO> dataList = new ArrayList<>();
- dataList.add(testDO);
- dataList.add(testDO1);
- dataList.add(testDO2);
- dataList.add(testDO3);
- Workbook workbook = DefaultExcelBuilder.getInstance().sheetName("default example").fieldDisplayOrder(order).build(dataList);
使用 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();
- }
- }
来源: http://www.bubuko.com/infodetail-2854901.html