poi-tl_区块对循环功能

文章介绍了如何利用DeepOove的poi-tl库处理Excel数据,将其转换并写入到Word模板中。通过添加依赖,定义ExcelBean对象,读取Excel文件,然后按照表格结构分组数据,最后使用LoopRowTableRenderPolicy渲染Word模板,生成新的Word文档。
摘要由CSDN通过智能技术生成

官网API

pom

<!-- 自动依赖org.apache.poi -->
<dependency>  
    <groupId>com.deepoove</groupId>  
    <artifactId>poi-tl</artifactId>  
    <version>1.10.0</version>  
</dependency>

word模板

模板内容
{{?list}}
{{tableName}}表

{{tables}}数据项英文名称数据项中文名称数据类型类型长度说明
[fieldOfEn][fieldOfCn][fieldType][fieldLength]

{{/list}}

代码实现

ExcelBean

import lombok.Data;
/**  
 * @FileName: ExcelBean  
 * @Description:  从Excel读取数据
 */
@Data  
public class ExcelBean {  
    @ExcelProperty("表名")  
    private String tableName;  
    @ExcelProperty("字段英文名称")  
    private String fieldOfEn;  
    @ExcelProperty("字段中文名称")  
    private String fieldOfCn;  
    @ExcelProperty("字段类型")  
    private String fieldType;  
    @ExcelProperty("类型长度")  
    private String fieldLength;  
}

Test Method

读取excel数据,进行转化写入word

public void test_Lord(){
	String path = "D:\\xuanji_dwd/资产.xlsx";  
	templateParse(path);
}

public void templateParse(String dataStandardExcelPath) {  
    try (FileInputStream fileInputStream = new FileInputStream(dataStandardExcelPath)) {  
        ZipSecureFile.setMinInflateRatio(0.001);  
        List<ExcelBean> fields = EasyExcel.read(fileInputStream, ExcelBean.class, null).doReadAllSync();  
        Map<String, List<ExcelBean>> groupMap = fields.stream().collect(Collectors.groupingBy(ExcelBean::getTableName));  
        List<Map<String,Object>> foreachList = new ArrayList<>();  
        groupMap.forEach((String tableName,List<ExcelBean> beans)->{  
            Map<String,Object> map = new HashMap<>();  
            map.put("tables",beans);  
            map.put("tableName",tableName);  
            foreachList.add(map);  
        });  
  
        Map<String,Object> resMap = new HashMap<>();  
        resMap.put("list",foreachList);  
  
        // 破解循环表格  
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();  
        Configure config = Configure.builder()  
                .bind("tables", policy)  
                .build();  
  
        XWPFTemplate.compile(new File("D:\\docx/template.docx"),config).render(resMap).writeAndClose(new FileOutputStream("D:\\docx/output.docx"));  
    } catch (FileNotFoundException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值