所有Excel构建器均不可单例化,务必注意!!!

模板构建,核心原理是根据模板引擎渲染出符合需求布局的Html Table元素,构建器迭代table元素中的单元格渲染出Excel单元格。

1.导出模板引擎选定

  1. 以下模板引擎默认均未被引入,使用者可根据自身需要选择在pom.xml中声明引入。

    The following template engine is not introduced by default except Beetl. Users can choose to introduce the introduction in pom.xml according to their needs.

  2. 以下模板引擎版本为最低版本号。

    The following template engine version is the lowest version number.

  1. <dependency>
  2. <groupId>com.ibeetl</groupId>
  3. <artifactId>beetl</artifactId>
  4. <version>2.7.23</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.freemarker</groupId>
  8. <artifactId>freemarker</artifactId>
  9. <version>2.3.23</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.codehaus.groovy</groupId>
  13. <artifactId>groovy-templates</artifactId>
  14. <version>2.4.13</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.thymeleaf</groupId>
  18. <artifactId>thymeleaf</artifactId>
  19. <version>2.1.6.RELEASE</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.velocity</groupId>
  23. <artifactId>velocity</artifactId>
  24. <version>1.7</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.jfinal</groupId>
  28. <artifactId>enjoy</artifactId>
  29. <version>4.8</version>
  30. </dependency>

2.Workbook生成

  1. 已存在Html文件时,使用这种方式,Html文件不局限于放在项目的classpath(如:resources)下,也无需模板引擎
  1. // get html file
  2. File htmlFile = new File("/Users/liaochong/Downloads/example.html");
  3. // read the html file and use default excel style to create excel
  4. Workbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build();
  5. // this is a example,you can write the workbook to any valid outputstream
  6. FileExportUtil.export(workbook, new File("/Users/liaochong/Downloads/excel.xlsx"));
  1. 使用内置的Freemarker等模板引擎Excel构建器,模板文件应当存放在classpath下,具体请参照项目中的example
  1. /**
  2. * use non-default-style excel builder
  3. * 模板文件放置在resources下
  4. *
  5. * @param response response
  6. */
  7. @GetMapping("/freemarker/example")
  8. public void build(HttpServletResponse response) {
  9. Map<String, Object> dataMap = this.getDataMap();
  10. try (ExcelBuilder excelBuilder = new FreemarkerExcelBuilder()) {
  11. Workbook workbook = excelBuilder
  12. // fileTemplate(dirPath,fileName)
  13. .classpathTemplate("/templates/freemarkerToExcelExample.ftl")
  14. .build(dataMap);
  15. AttachmentExportUtil.export(workbook, "freemarker_excel", response);
  16. }
  17. }
  18. /**
  19. * use default-style excel builder
  20. * 模板文件放置在resources下
  21. *
  22. * @param response response
  23. */
  24. @GetMapping("/freemarker/defaultStyle/example")
  25. public void buildWithDefaultStyle(HttpServletResponse response) {
  26. Map<String, Object> dataMap = this.getDataMap();
  27. try (ExcelBuilder excelBuilder = new FreemarkerExcelBuilder()){
  28. Workbook workbook = excelBuilder
  29. // fileTemplate(dirPath,fileName)
  30. .classpathTemplate("/templates/freemarkerToExcelExample.ftl")
  31. .useDefaultStyle()
  32. .build(dataMap);
  33. AttachmentExportUtil.export(workbook, "freemarker_excel", response);
  34. }
  35. }
  36. private Map<String, Object> getDataMap() {
  37. Map<String, Object> dataMap = new HashMap<>();
  38. dataMap.put("sheetName", "freemarker_excel_example");
  39. List<String> titles = new ArrayList<>();
  40. titles.add("Category");
  41. titles.add("Product Name");
  42. titles.add("Count");
  43. dataMap.put("titles", titles);
  44. List<Product> data = new ArrayList<>();
  45. for (int i = 0; i < 10; i++) {
  46. Product product = new Product();
  47. if (i % 2 == 0) {
  48. product.setCategory("蔬菜");
  49. product.setName("小白菜");
  50. product.setCount(100);
  51. } else {
  52. product.setCategory("电子产品");
  53. product.setName("ipad");
  54. product.setCount(999);
  55. }
  56. data.add(product);
  57. }
  58. dataMap.put("data", data);
  59. return dataMap;
  60. }

3.模板示例

  1. <table>
  2. <caption>${sheetName}</caption>
  3. <thead>
  4. <tr style="background-color: #6495ED">
  5. <th colspan="3" style="text-align: center;vertical-align: middle;font-weight: bold;font-size: 14px;">产品介绍</th>
  6. </tr>
  7. <tr>
  8. <#list titles as title>
  9. <th>${title}</th>
  10. </#list>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <#list data as item>
  15. <tr>
  16. <td>${item.category}</td>
  17. <td>${item.name}</td>
  18. <td>${item.count}</td>
  19. <td url>百度地址</td>
  20. </tr>
  21. </#list>
  22. </tbody>
  23. </table>