使用 easypoi 生成 excel

634 阅读1分钟

1、easypoi 概述

easypoi 开源地址

开发指南地址

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法

关于Excel导出XLS和XLSX区别

1.导出时间XLS比XLSX快2-3倍
2.导出大小XLS是XLSX的2-3倍或者更多
3.导出需要综合网速和本地速度做考虑^~^

2、添加依赖

<properties>
    <easypoi.version>4.1.0</easypoi.version>
</properties>

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>${easypoi.version}</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>${easypoi.version}</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>${easypoi.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${easypoi.version}</version>
</dependency>

3、基于模板来生成

模板文件如下:

image.png

生成代码如下:

String templatePath = emailTemplate + File.separatorChar + "key_info.xls";
TemplateExportParams templateExportParams = new TemplateExportParams(templatePath);

Map<String, Object> map = new HashMap<>();
map.put("productList",keyFileListModels);
Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams,map);

if (!FileUtil.exist(dirPath)) {
   FileUtil.mkdir(dirPath);
}
String outPath = dirPath + File.separatorChar + "KEY文件环境及项目信息对照表.xls";
FileOutputStream fileOutputStream = new FileOutputStream(new File(outPath));
workbook.write(fileOutputStream);
workbook.close();
fileOutputStream.close();

4、基于 html 模板来生成

注意,基于html 生成的 excel table 标签上要有 "sheetName" 属性,对于样式,目前也只支持内联样式。

attach.html 为html 模板文件,内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>产品发货清单</title>


</head>
<body>


<table sheetName="产品发货清单">
    <tbody>
    <tr>
        <td style="width: 20px;height: 30px;font-size: 13px;">签收单编号</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.signOrderCode!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">买方编号</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.buyerNum!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">签约客户名称</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.customerCompanyName!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">项目名称</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.projectName!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">最终用户名称</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.endCustomerName!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">合同乙方</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.contractB!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">接收邮箱</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.customerEmail!""}</td>
    </tr>
    <tr>
        <td style="width: 20px;height: 15px;font-size: 13px;">发货日期</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">${productApply.outboundDate?string("yyyy-MM-dd")}</td>
    </tr>


    <tr>
        <td style="width: 20px;height: 25px"></td>
        <td colspan="4" style="width: 40px;height: 25px"></td>
    </tr>

    <tr>
        <td style="width: 20px;height: 25px;font-size: 13px;">序号</td>
        <td colspan="4" style="width: 30px;height: 25px;font-size: 13px;">交货清单</td>
    </tr>


        <#list productInfos as p>

        <tr>
            <td rowspan="4" style="border: 1px ;font-size: 13px;">${p_index + 1}</td>
            <td style="width: 10px;height: 35px;font-size: 13px;">产品名称 </td>
            <td style="width: 30px;height: 35px;font-size: 13px; word-break: break-all; word-wrap:break-word;">${p.productName}</td>
            <td style="width: 10px;height: 35px;font-size: 13px;">版本</td>
            <td style="width: 30px;height: 35px;font-size: 13px;word-break: break-all; word-wrap:break-word;">${p.productVersion}</td>
        </tr>



        <tr>
            <td style="width: 10px;height: 25px;font-size: 13px;">单位</td>
            <td style="width: 30px;height: 25px;font-size: 13px;word-break: break-all; word-wrap:break-word;">${p.productUnit!"-"}</td>
            <td style="width: 10px;height: 25px;font-size: 13px;">数量</td>
            <td style="width: 30px;height: 25px;font-size: 13px;">${p.productCount!"-"}</td>
        </tr>

        <tr>
            <td style="width: 10px;height: 25px;font-size: 13px;">产品编码</td>
            <td  colspan="3"  style="width: 30px;height: 25px;font-size: 13px;"> ${p.productCode!""}</td>
        </tr>

        <tr>
            <td style="width: 10px;height: 25px;font-size: 13px;">授权卡编号</td>
            <td colspan="3" style="width: 10px;height: 25px;font-size: 13px;">${p.productAuthCode!""}</td>
        </tr>

    </#list>

    </tbody>
</table>


</body>
</html>


生成代码如下:

String templatePath = emailTemplate + File.separatorChar + "attach.html";

TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig());

String templateData = FileUtil.readString(templatePath, Charset.forName("utf-8"));

Template template = engine.getTemplate(templateData);

Map<String, Object> map = new HashMap<>();
map.put("productApply",productApply);
map.put("productInfos",productInfos);
// 获取模板数据
String result = template.render(map);

String dirPath = emailTemplate + File.separatorChar  + productApply.getId();
if (!FileUtil.exist(dirPath)) {
   FileUtil.mkdir(dirPath);
}
String outPath = dirPath + File.separatorChar + "产品发货清单.xls";

Workbook workbook = ExcelXorHtmlUtil.htmlToExcel(result, ExcelType.XSSF);
FileOutputStream fileOutputStream = new FileOutputStream(new File(outPath));
workbook.write(fileOutputStream);
workbook.close();
fileOutputStream.close();

输出如下:

image.png

5、总结

对于一般的表格,可以基于模板来生成 Excel,可以简化样式的操作。

对于比较复杂的表格,数据有很多基于循环的,可以考虑使用 html -> Excel