Java操作word模板插入圖片


Java操作word模板插入圖片

在word模板中寫上圖片語法,插入本地圖片,同時還支持網絡圖片,定義圖片大小。

1.制作word模板,插入圖片使用語法@,支持插入本地圖片和網絡圖片{{@localPicture}} {{@urlPicture}}

2.使用Poi-tl,添加依賴

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

poi-tl是一個java的模板引擎。

GitHub主頁:https://github.com/Sayi/poi-tl
文檔地址:http://deepoove.com/poi-tl

3.Java代碼示例

public void testNumbericRender() throws Exception {
        Map<String, Object> datas = new HashMap<String, Object>() {
            {
                //本地圖片
                put("localPicture", new PictureRenderData(100, 120, "src/test/resources/logo.png"));
                //網路圖片 
                put("urlPicture", new PictureRenderData(100, 100, ".png", BytePictureUtils.getUrlByteArray("https://avatars3.githubusercontent.com/u/1394854?v=3&s=40")));
            }
        };

        XWPFTemplate template = XWPFTemplate.compile("~/picture.docx")
                .render(datas);

        FileOutputStream out = new FileOutputStream("out_picture.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }


免責聲明!

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



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