很多小伙伴在工作中,可能又這樣一個需求:根據(jù)word模板去填充數(shù)據(jù),變成我們想要的word文檔,這是很多剛進(jìn)入職場的小白都會碰到的需求。
當(dāng)遇上這種需求,我們可以通過這篇文章要講的poi-tl 來做處理。
導(dǎo)入依賴
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.1</version>
</dependency>
填充文本
首先,我們需要先準(zhǔn)備一個word文檔,必須是.docx結(jié)尾的哦。
我們先在指定的位置創(chuàng)建一個測試文檔。我這邊直接在springboot中的templates目錄下創(chuàng)建一個名為test1.docx的文件。
這里需要注意:對于文本數(shù)據(jù)而言,動態(tài)填充的數(shù)據(jù),用{{}}包裹即可。
//掌握如何填充文本類型數(shù)據(jù)
@Test
void contextLoads() throws IOException {
String path="D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\test1.docx";
//第一步:讀取模板
XWPFTemplate compile = XWPFTemplate.compile(path);
//第二步:創(chuàng)建數(shù)據(jù),用于把我們模板中的{{}}包裹的變量替換掉,map中保存的key一定要和模板中的變量保持一致
HashMap<String, Object> map = new HashMap<>();
map.put("date", LocalDate.now());
compile.render(map);
//第三步:把設(shè)置的參數(shù)寫入,并生成新的word文檔
compile.writeToFile("D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\text.docx");
compile.close();
}
執(zhí)行上面代碼,會發(fā)現(xiàn)我們多了一個text.docx 的文檔,打開看,發(fā)現(xiàn)數(shù)據(jù)已經(jīng)被填充了。
填充圖片
還是一樣,我們創(chuàng)建一個用于填充數(shù)據(jù)的模板。不過對于文本而言,填充設(shè)置的參數(shù)需要用{{@}}來包含住。
我們先在指定的位置創(chuàng)建一個測試文檔。我這邊直接在springboot中的templates目錄下創(chuàng)建一個名為imgTemplate.docx的文件。
imgTemplate文件打開是這樣的:
當(dāng)執(zhí)行下面通過poi-tl實現(xiàn)的填充圖片代碼:
@Test
void test2() throws IOException {
String path="D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\imgTemplate.docx";
//第一步:讀取模板
XWPFTemplate compile = XWPFTemplate.compile(path);
//第二步:創(chuàng)建數(shù)據(jù),用于把我們模板中的{{}}包裹的變量替換掉,map中保存的key一定要和模板中的變量保持一致
HashMap<String, Object> map = new HashMap<>();
//本地圖片
//120,120:代表設(shè)圖片的長寬
map.put("img1",new PictureRenderData(120,120,"D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\img.png"));
//流圖片
map.put("img2",new PictureRenderData(120,120,".png",new FileInputStream("D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\img.png")));
//網(wǎng)絡(luò)圖片
map.put("img3", new PictureRenderData(100, 100, ".jpg", BytePictureUtils.getUrlBufferedImage("https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/314e251f95cad1c8ba1e530c7d3e6709c93d5177.jpg")));
compile.render(map);
compile.writeToFile("D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\Img.docx");
compile.close();
}
打開img.docx文件,發(fā)現(xiàn)圖片已經(jīng)被填充進(jìn)去了
填充表格
填充表格的時候,需要使用{{*var}}來填充表格。
還是一樣,在指定的位置創(chuàng)建一個測試文檔。我這邊直接在springboot中的templates目錄下創(chuàng)建一個名為TableTemplate.docx的文件。而文件中的內(nèi)容為:
然后,編寫測試方法,執(zhí)行以下代碼,進(jìn)行表格的填充
@Test
public void test() throws IOException {
String path="D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\tableTemplate.docx";
//第一步:讀取模板
XWPFTemplate compile = XWPFTemplate.compile(path);
//第二步:創(chuàng)建數(shù)據(jù),用于把我們模板中的{{}}包裹的變量替換掉,map中保存的key一定要和模板中的變量保持一致
HashMap<String, Object> map = new HashMap<>();
//設(shè)置單元格,表頭
RowRenderData header = RowRenderData.build(new TextRenderData("6495ED", "姓名"), new TextRenderData("6495ED", "學(xué)歷"));
//設(shè)置表內(nèi)容,如果在實際應(yīng)用中,傳入一個list集合或者數(shù)組,方可用foreach進(jìn)行循環(huán)填充
RowRenderData row0 = RowRenderData.build("張三", "研究生");
RowRenderData row1 = RowRenderData.build(new TextRenderData("李四"), new TextRenderData("博士"));
RowRenderData row2 = RowRenderData.build("王五", "博士后");
map.put("table", new MiniTableRenderData(header, Arrays.asList(row0, row1, row2)));
compile.render(map);
compile.writeToFile("D:\\QQ_File\\1719036792\\FileRecv\\tuanfei\\src\\main\\resources\\templates\\table.docx");
compile.close();
}
執(zhí)行成功后,在生成文件位置找到對應(yīng)的文件,打開查看:
執(zhí)行成功。。。。。
在實際開發(fā)中,我們經(jīng)常會使用到Word的導(dǎo)出和數(shù)據(jù)填充。使用poi-tl,我們可以很輕松的就能對word文件進(jìn)行操作。我寫的這篇文章中只對我們常用的三種類型進(jìn)行講解。當(dāng)我們需要更加了解和使用的時候,還是需要查看官方文檔能更一步的了解和掌握poi-tl。文章來源:http://www.zghlxwxcb.cn/news/detail-732680.html
POI-TL1.5.x的文檔地址:點擊進(jìn)入文檔地址文章來源地址http://www.zghlxwxcb.cn/news/detail-732680.html
到了這里,關(guān)于輕松學(xué)會Java導(dǎo)出word,一篇文章就夠了!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!