poi-tl循環(huán)生成word表格簡單實現(xiàn)(一個模板)

話不多說,上模板

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

 //需要循環(huán)的變量配置
        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, "數(shù)據庫設計文檔.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;// 數(shù)據類型

    private String lower_camel; //單峰駝
    private String upper_camel; //雙峰駝

    private Integer length; //字符長度
    private String column_key;//鍵

    private String dbType; //數(shù)據庫類型
    private String allTypeName;//類型全稱
    private String mb_db_type ;//mybatis 數(shù)據庫類型

}

輸出效果圖

image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容