标签:poi tst user 2.7 cat org 属性 etag default
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>
优点:
可选模板:
<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>
示例:
// 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);
/** * 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(); } }
标签:poi tst user 2.7 cat org 属性 etag default
原文地址:https://www.cnblogs.com/liaochong/p/html2excel.html