java通過word模板生成word文檔


介紹

上次公司項目需要一個生成word文檔的功能,有固定的模板根據業務填充數據即可,由於從來沒做過,項目也比較着急於是去網上找有沒有合適的工具類,找了好幾種,看到其中有freeMark模板生成比較靠譜於是采用這個,正常生成成功了還挺高興的於是修改優化部署測試,出問題了,由於我一直使用wps可以正常打開,但是同事使用office打不開,於是各種查找原因都沒好,於是只能轉變思路又試了兩種還是不好用,直到發現這款模板生成 poi-tl 真的做的很不錯,而且是國人寫的,關於學習這個東西還是看官方文檔的好。
文檔地址: http://deepoove.com/poi-tl

源碼

源碼查看規則
源碼位置: blog-study:static-utils - word
在源碼里可以完整的操作

代碼展示

public static void main(String[] args) {
        //模板、文件、圖片路徑
        String workPath=System.getProperty("user.dir") + "/static-utils/src/main/resources/word/";
        String templateName="test.docx";
        Map<StringObject> datas = new HashMap<StringObject>() {
            {
                //文本
                put("name","xiaoguo");
                put("sex","男");
                put("year","20200105");
                put("hello","xiaoguo寫於2020年一月");

                //自定義表格
                RowRenderData header = RowRenderData.build(new TextRenderData("1C86EE""姓名"), new TextRenderData("1C86EE""學歷"));
                RowRenderData row0 = RowRenderData.build("張三""研究生");
                RowRenderData row1 = RowRenderData.build("李四""博士");
                RowRenderData row2 = RowRenderData.build("王五""博士后");
                put("tables"new MiniTableRenderData(header, Arrays.asList(row0, row1, row2)));

                //自定義有序列表
                put("testText"new NumbericRenderData(NumbericRenderData.FMT_DECIMAL, new ArrayList<TextRenderData>() {
                    {
                        add(new TextRenderData("Plug-in grammar"));
                        add(new TextRenderData("Supports word text, header..."));
                        add(new TextRenderData("Not just templates, but also style templates"));
                    }
                }));

                //網落圖片
                put("picture"new PictureRenderData(200150".jpg", BytePictureUtils.getUrlBufferedImage("https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=61c551093f6d55fbd1cb7e740c4b242f/d8f9d72a6059252d937820d3369b033b5ab5b9fd.jpg")));
                //本地圖片
                put("picture2"new PictureRenderData(200150".jpg", BytePictureUtils.getLocalByteArray(new File(workPath + "c1.jpg"))));

            }
        };

        generateWord(datas, workPath + templateName, workPath);
    }


    /**
     * 通過word模板並生成word文檔
     *
     * @param paramData    參數數據
     * @param templatePath word模板地址加模板文件名字
     * @param outFilePath  輸出文件地址(不帶文件名字)
     * @return 生成的word文件
     */

    public static File generateWord(Map<StringObject> paramData, String templatePath, String outFilePath) {
        String outFileName = "word_" + System.currentTimeMillis() + "_" + random.nextInt(100) + ".doc";
        return generateWord(paramData, templatePath, outFilePath, outFileName);
    }


    /**
     * 通過word模板並生成word文檔
     *
     * @param paramData    參數數據
     * @param templatePath word模板地址加模板文件名字
     * @param outFilePath  輸出文件地址(不帶文件名字)
     * @param outFileName  輸出文件名字
     * @return 生成的word文件
     */

    public static File generateWord(Map<StringObject> paramData, String templatePath, String outFilePath, String outFileName) {
        //判斷輸出文件路徑和文件名是否含有指定后綴
        outFilePath = CommonUtil.addIfNoSuffix(outFilePath, "/""\\");
        outFileName = CommonUtil.addIfNoSuffix(outFileName, ".doc"".docx");
        //解析word模板
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramData);
        //輸出文件
        FileOutputStream out = null;
        File outFile = new File(outFilePath + outFileName);
        try {
            out = new FileOutputStream(outFile);
            template.write(out);
            out.flush();
        } catch (IOException e) {
            log.error("生成word寫入文件失敗", e);
        } finally {
            if (template != null) {
                try {
                    template.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return outFile;
    }


免責聲明!

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



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