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.

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

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.  
  7. <dependency>
  8. <groupId>org.freemarker</groupId>
  9. <artifactId>freemarker</artifactId>
  10. <version>2.3.23</version>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>org.codehaus.groovy</groupId>
  15. <artifactId>groovy-templates</artifactId>
  16. <version>2.4.13</version>
  17. </dependency>
  18.  
  19. <dependency>
  20. <groupId>org.thymeleaf</groupId>
  21. <artifactId>thymeleaf</artifactId>
  22. <version>2.1.6.RELEASE</version>
  23. </dependency>

2.Workbook生成

  • 已存在Html文件时,使用这种方式,Html文件不局限于放在项目的classpath(如:resources)下,也无需模板引擎
  1. // get html file
  2. File htmlFile = new File("/Users/liaochong/Downloads/example.html");
  3.  
  4. // read the html file and use default excel style to create excel
  5. Workbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build();
  6.  
  7. // this is a example,you can write the workbook to any valid outputstream
  8. FileExportUtil.export(workbook, new File("/Users/liaochong/Downloads/excel.xlsx"));
  • 使用内置的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. .template("/templates/freemarkerToExcelExample.ftl")
  13. .build(dataMap);
  14. AttachmentExportUtil.export(workbook, "freemarker_excel", response);
  15. }
  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. .template("/templates/freemarkerToExcelExample.ftl")
  30. .useDefaultStyle()
  31. .build(dataMap);
  32. AttachmentExportUtil.export(workbook, "freemarker_excel", response);
  33. }
  34. }
  35.  
  36. private Map<String, Object> getDataMap() {
  37. Map<String, Object> dataMap = new HashMap<>();
  38. dataMap.put("sheetName", "freemarker_excel_example");
  39.  
  40. List<String> titles = new ArrayList<>();
  41. titles.add("Category");
  42. titles.add("Product Name");
  43. titles.add("Count");
  44. dataMap.put("titles", titles);
  45.  
  46. List<Product> data = new ArrayList<>();
  47. for (int i = 0; i < 10; i++) {
  48. Product product = new Product();
  49. if (i % 2 == 0) {
  50. product.setCategory("蔬菜");
  51. product.setName("小白菜");
  52. product.setCount(100);
  53. } else {
  54. product.setCategory("电子产品");
  55. product.setName("ipad");
  56. product.setCount(999);
  57. }
  58. data.add(product);
  59. }
  60. dataMap.put("data", data);
  61. return dataMap;
  62. }