Java 采用Poi-Tl实现word导出示例

Java 采用 Poi-Tl Word模板引擎导出word表格示例

 官方poi-tl 的教程地址 :  poi-tl

 

  1. 准备一个word模板 templete.docx,放在resource文件夹下。具体路径在代码中有体现。
  2. 模板中插入表格格式2列4行就行。
  3. 编写插件类   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());
    			}
    		}
    	}
    }
    

     

  4. 新建一个测试类 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();
    	}
    
    }
    

     

  5. 最后导出word文档

     喜欢看源码的童鞋点一下:gitee地址

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值