poi-tl循环生成word表格简单实现(一个模板)

话不多说,上模板

image.png

pom.xml

  <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
        </dependency>

测试代码

参考 https://blog.csdn.net/qq_26383975/article/details/112238802

 //需要循环的变量配置
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder()
                .bind("tables", policy)
                .bind("flist",policy)
                .build();
        //组装对象
        List<JSONObject> tableVars = tables.stream().map(item ->{
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(item));
            try {
                jsonObject.put("flist",getFields(item,propConfig));
                jsonObject.put("title",item.getComment());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(jsonObject);
            return jsonObject;
        }).collect(Collectors.toList());
        //渲染模板
        XWPFTemplate template = XWPFTemplate.compile(this.getClass().getResourceAsStream("/templates/onedoctemplate.docx"), config).render(
                new HashMap<String, Object>() {{
                    put("title",propConfig.getModuleName());
                    put("tables", tableVars);
                    put("date",new SimpleDateFormat("yyyy-MM-DD HH:mm:ss").format(new Date()));
                    put("author",propConfig.getAuthor());
                }}
        );
        //输出
        String outDir = propConfig.getConfigDir().concat(File.separator).concat("dist");
        File dir = new File(outDir);
        dir.mkdirs();

        File file = new File(dir, "数据库设计文档.docx");
        if (file.exists() && file.isFile()) {
            file.delete();
        } 
        FileOutputStream out = new FileOutputStream(file);
        template.write(out);
        out.flush();
        out.close();
        template.close();

实体类说明: table

import lombok.Data;

@Data
public class Table {
    private String t_name;// 表名
    private String comment;// 表名注释
    private String cls_upp;// 类名(首字母大写)
    private String c_name;// 类中文名
}

实体类说明: field

import lombok.Data;

@Data
public class Field {
    private String name;// 字段名
    private String comment;// 注释
    private String type;// 数据类型

    private String lower_camel; //单峰驼
    private String upper_camel; //双峰驼

    private Integer length; //字符长度
    private String column_key;//键

    private String dbType; //数据库类型
    private String allTypeName;//类型全称
    private String mb_db_type ;//mybatis 数据库类型

}

输出效果图

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。