word模板生成利器poi-tl


介紹

大家好,我是知識追尋者,今天給大家介紹一款word模板生成理器;
工作中經常會遇到這種情況,將后台的數據填充到word模板,然后生渲染模板生成新的word提供下載;比如學生成績單,單位合同,報銷費用等!如果能夠掌控一款合適的java生成word模板的工具,將極大的提高我們的開發效率!

官方文檔:http://deepoove.com/poi-tl

為什么選擇 poi-tl

內容支持

引擎功能 描述
文本 將標簽渲染為文本
圖片 將標簽渲染為圖片
表格 將標簽渲染為表格
列表 將標簽渲染為列表
圖表 條形圖(3D條形圖)、柱形圖(3D柱形圖)、面積圖(3D面積圖)、折線圖(3D折線圖)、雷達圖、餅圖(3D餅圖)等圖表渲染
If Condition判斷 隱藏或者顯示某些文檔內容(包括文本、段落、圖片、表格、列表、圖表等)
Foreach Loop循環 循環某些文檔內容(包括文本、段落、圖片、表格、列表、圖表等)
Loop表格行 循環渲染表格的某一行
Loop表格列 循環渲染表格的某一列
Loop有序列表 支持有序列表的循環,同時支持多級列表
圖片替換 將原有圖片替換成另一張圖片
書簽、錨點、超鏈接 支持設置書簽,文檔內錨點和超鏈接功能
強大的表達式 完全支持SpringEL表達式,可以擴展更多的表達式:OGNL, MVEL…
標簽定制 支持自定義標簽前后綴
文本框 文本框內標簽支持
樣式 模板即樣式,同時代碼也可以設置樣式
模板嵌套 模板包含子模板,子模板再包含子模板
合並 Word合並Merge,也可以在指定位置進行合並
用戶自定義函數(插件) 在文檔任何位置執行函數

核心代碼

核心代碼,將數據填充入模板生成新的word;

 private static Logger logger = LoggerFactory.getLogger(TemplateController.class);


   /**
    * @author lsc
    * @param templatePath word模板文件路徑
    * @param fileDir      生成的文件存放地址
    * @param fileName     生成的文件名
    * @param paramMap     參數集合
    * @return 返回word生成的路徑
    */
    public static String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap) {
        Assert.notNull(templatePath, "word模板文件路徑不能為空");
        Assert.notNull(fileDir, "生成的文件存放地址不能為空");
        Assert.notNull(fileName, "生成的文件名不能為空");
        File dir = new File(fileDir);
        if (!dir.exists()) {
            logger.info("目錄不存在,創建文件夾{}!", fileDir);
            dir.mkdirs();
        }
        String filePath = fileDir +"\\"+ fileName;
        // 讀取模板渲染參數
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramMap);
        try {
            // 將模板參數寫入路徑
            template.writeToFile(filePath);
            template.close();
        } catch (Exception e) {
            logger.error("生成word異常{}", e.getMessage());
            e.printStackTrace();
        }
        return filePath;
    }

渲染字符串

模板 {{value}}

渲染代碼

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        // 渲染文本

        params.put("dep","知識研發中心");
        params.put("apply_man","知識追尋者");
        params.put("project","搭建個人網站 https://zszxz.com/index 費用");
        params.put("money","19998");
        params.put("count","8");
        params.put("year","2020");
        params.put("month","02");
        params.put("day","8");
		
    	// 模板路徑
        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路徑
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";

        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文檔路徑:" + wordPath);
    }

生成示例

渲染表格

表格模板 {{#table0}}

渲染代碼

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();

        params.pables.of(new String[][] {
                        new String[] { "19998", "8" ,"apply_man","搭建個人網站 https://zszxz.com/index 費用"},
                        new String[] { "19998", "8" ,"apply_man","搭建個人網站 https://zszxz.com/index 費用"},
                }).border(TableStyle.BorderStyle.DEFAULT).create
        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路徑
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";

        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文檔路徑:" + wordPath);
    }

渲染結果

循環渲染

此類渲染的表格存在一些問題不能根據我們定義的模板循環渲染,比如費用報銷明細里面多條記錄,此時就需要使用到表格循環;

模板

實體類

/**
 * @author lsc
 * <p> </p>
 */
public class Money {

    private String apply_man;

    private String project;

    private String money;

    private String count;
    // 省略set get
}    

核心代碼

public static void main(String[] args) throws IOException {
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Money money1 = new Money();
        money1.setApply_man("知識追尋者");
        money1.setProject("搭建個人網站 https://zszxz.com/index 費用");
        money1.setCount("8");
        money1.setMoney("19988");
        Money money2 = new Money();
        money2.setApply_man("知識追尋者");
        money2.setProject("參加晚會費用");
        money2.setCount("8");
        money2.setMoney("98000");
        List<Money> moneys = new ArrayList<>();
        moneys.add(money1);
        moneys.add(money2);
        // 插件綁定
        Configure config = Configure.builder()
                .bind("moneys", policy).build();
        String templatePath = "C:/mydata/generator/demo/template.docx";
        XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(
                new HashMap<String, Object>() {{
                    put("moneys", moneys);
                }}
        );
        String filePath = "C:/mydata/generator/demo/zszxz.docx";
        template.writeToFile(filePath);
        template.close();
    }

生成結果

渲染列表

模板 {{*list}}

渲染代碼

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();

        params.put("list", Numberings.create("Plug-in grammar",
                "Supports word text, pictures, table...",
                "Not just templates"));

        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路徑
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";

        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文檔路徑:" + wordPath);
    }

渲染結果

最后

需要 本套教程 word模板 關注公眾號:知識追尋者 回復 template 模板 獲取調試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM