Java 采用 Poi-Tl Word模板引擎导出word表格示例
官方poi-tl 的教程地址 : poi-tl
- 准备一个word模板 templete.docx,放在resource文件夹下。具体路径在代码中有体现。
- 模板中插入表格格式2列4行就行。
- 编写插件类 TableRenderPolicy.java
package com.example.demo.poitl.policy; import java.util.List; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import com.deepoove.poi.data.RowRenderData; import com.deepoove.poi.policy.DynamicTableRenderPolicy; /** * @auther 作者 大有可为 * @version 创建时间2019年7月6日下午2:40:28 * @mail 邮箱 cdf30088@163.com * @weixin 微信 wxid_9w1tw6vprylc22 * @desc 功能描述 表格内容填充 自定义插件 */ public class TableRenderPolicy extends DynamicTableRenderPolicy { @Override public void render(XWPFTable table, Object data) { //数据强制转换一下 List<RowRenderData> rowList = (List<RowRenderData>) data; //判断要填充的数据行数是否超出现有的表格行数 超出的话 进行插入新行提前插入 if (rowList.size() > table.getRows().size()) { for (int i = 0; i < rowList.size() - table.getRows().size(); i++) { XWPFTableRow insertNewTableRow = table.insertNewTableRow(0); for (int j = 0; j < 2; j++) insertNewTableRow.createCell(); } } //循环数据 将数据一次放入每个单元格 for (int i = 0; i < rowList.size(); i++) { for (int j = 0; j < 2; j++) { table.getRow(i).getCell(j).setText(rowList.get(i).getCellDatas().get(j).getRenderData().getText()); } } } }
-
新建一个测试类 TableRenderTest.java
package com.example.demo.poitl; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.tuple.Pair; import org.junit.Test; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat.Enum; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; import com.deepoove.poi.config.Configure.ConfigureBuilder; import com.deepoove.poi.data.RowRenderData; import com.example.demo.poitl.policy.TableRenderPolicy; /** * @auther 作者 大有可为 * @version 创建时间2019年7月6日上午9:09:28 * @mail 邮箱 cdf30088@163.com * @weixin 微信 wxid_9w1tw6vprylc22 * @desc 功能描述 表格填充数据测试 */ public class TableRenderTest { /** * ● ● ● */ public static final Pair<Enum, String> FMT_CHOOSE = Pair.of(STNumberFormat.NONE, "☑"); /** * ● ● ● */ public static final Pair<Enum, String> FMT_BLOCK = Pair.of(STNumberFormat.NONE, "□"); /** * @throws Exception */ @Test public void testNumbericRender() throws Exception { Map<String, Object> tableDataMap = new HashMap<String, Object>(); List<RowRenderData> rowList = new ArrayList<RowRenderData>(); new RowRenderData(); rowList.add(RowRenderData.build("□篮球","□足球")); new RowRenderData(); rowList.add(RowRenderData.build("□是","□否")); new RowRenderData(); rowList.add(RowRenderData.build("□玩过","□听说过")); new RowRenderData(); rowList.add(RowRenderData.build("□好开心","□好失望")); //绑定渲染位置 tableDataMap.put("内容", rowList); ConfigureBuilder builder = Configure.newBuilder(); //自定义定位符 builder.buildGramer("${", "}"); //自定义插件渲染 builder.customPolicy("内容", new TableRenderPolicy()); //读取模板 XWPFTemplate xwpfTemplate = XWPFTemplate.compile("src/main/resources/file/word/template.docx", builder.build()); //渲染 xwpfTemplate.render(tableDataMap); FileOutputStream outputStream = new FileOutputStream(new File("src/main/resources/file/export/out_template.docx")); xwpfTemplate.write(outputStream); outputStream.flush(); outputStream.close(); xwpfTemplate.close(); } }
-
最后导出word文档
喜欢看源码的童鞋点一下:gitee地址