Skip to content

A toolkit for converting tables in html to excel, support .xls, .xlsx format, support personalization of background color, border, font, etc,this toolkit will work with table that has merged cells.(转换HTML表格为Excel的工具包,支持.xls、.xlsx格式,支持对背景色、边框、字体等进行个性化设置,支持合并单元格)

License

Notifications You must be signed in to change notification settings

muniao/html2excel

 
 

Repository files navigation

html2excel 文档

Build Status Maven Central License

html2excel是一款将html中表格(table)转化成excel的工具包.

版本支持 | Support Version

  • All version - only support for Java 8+

优点 | Advantages

  • 零学习成本:使用html作为模板,学习成本几乎为零;
  • 可生成任意复杂表格:本工具使用迭代单元格方式进行excel绘制,可生成任意复杂度excel;
  • 屏蔽POI操作:不直接操作过重的POI;
  • 支持常用背景色、边框、字体等样式设置:具体参见详情部分;
  • 支持.XLS、.XLSX:支持生成.xls、.xlsx后缀的excel;
  • 支持多种模板引擎:支持Freemarker、Groovy、Beetl等常用模板引擎,默认内置Beetl模板引擎(推荐引擎,Beetl文档);
  • 提供默认模板引擎,直接输出简单excel:无需编写任何html,已内置默认模板,可直接根据POJO数据列表输出;
  • 支持一次生成多sheet:以table作为sheet单元,支持一份excel文档中多sheet;

注意 | Attention

目前只支持模板文件存放在classpath下

Maven 依赖

<dependency>
    <groupId>com.github.liaochong</groupId>
    <artifactId>html2excel</artifactId>
    <version>x.x.x(参见上方最新版本)</version>
</dependency>

可选模板 | Template

  1. 以下模板引擎除Beetl外默认均未被引入,使用者可根据自身需要选择在pom.xml中声明引入;
  2. 以下模板引擎版本为最低版本号;
<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>

支持样式 | Support Style

CSS样式名称 样式值 示例
background-color 十六进制:#FFFFFF、RGB:rgb(0,255,255)、常用背景色名称:green,推荐使用十六进制 style="background-color:#ABFF00"
CSS样式名称 样式值 示例
border-top-style thin、dashed、medium、dotted、thick、double、hair、medium_dashed、dash_dot style="border-top-style:thin"
border-right-style thin、dashed、medium、dotted、thick、double、hair、medium_dashed、dash_dot style="border-top-style:thin"
border-bottom-style thin、dashed、medium、dotted、thick、double、hair、medium_dashed、dash_dot style="border-top-style:thin"
border-left-style thin、dashed、medium、dotted、thick、double、hair、medium_dashed、dash_dot style="border-top-style:thin"
CSS样式名称 样式值 示例
font-size 数值:14px(只会截取数值) style="font-size:14px"
font-family Times New Roman(不支持备选字体) style="font-family:Times New Roman"
font-style italic style="font-style:italic"
font-weight bold style="font-weight:bold"
CSS样式名称 样式值 示例
text-align general,left,center,right,fill,justify,center_selection,distributed style="text-align:center"
vertical-align top,center,bottom,justify,distributed style="vertical-align:center"

示例 | Example

  1. 已存在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);
  1. 使用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();
        }
    }
  1. 默认模板引擎使用
// 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")
                                    .titles(titles)
                                    .fieldDisplayOrder(order)
                                    .build(dataList);
  1. Excel sheet名称设置:table中添加 <caption>Monthly savings</caption>

可操作项 | Operational item

操作方法 说明
useDefaultStyle 使用默认样式,忽略html文件中自定义样式
freezePanes 固定窗口,可固定指定的行列

扩展 | Extend

如需使用非Freemarker、Groovy、Beetl模板引擎,可自行扩展,扩展规则如下:

  1. 继承 ExcelBuilder 抽象类;
  2. 实现抽象方法  ExcelBuilder getTemplate(String path)Workbook build(Map<String, Object> data)

About

A toolkit for converting tables in html to excel, support .xls, .xlsx format, support personalization of background color, border, font, etc,this toolkit will work with table that has merged cells.(转换HTML表格为Excel的工具包,支持.xls、.xlsx格式,支持对背景色、边框、字体等进行个性化设置,支持合并单元格)

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 83.6%
  • FreeMarker 15.2%
  • HTML 1.2%