poi-tl是什么
poi-tl是一個基于Apache POI的Word模板引擎,也是一個免費開源的Java類庫,你可以非常方便的加入到你的項目中,并且擁有著讓人喜悅的特性。
為什么選擇poi-tl
方案 | 移植性 | 功能性 | 易用性 |
---|---|---|---|
Poi-tl | Java跨平臺 | Word模板引擎,基于Apache POI,提供更友好的API | 低代碼,準(zhǔn)備文檔模板和數(shù)據(jù)即可 |
Apache POI | Java跨平臺 | Apache項目,封裝了常見的文檔操作,也可以操作底層XML結(jié)構(gòu) | 文檔不全,這里有一個教程:Apache POI Word快速入門 |
Freemarker | XML跨平臺 | 僅支持文本,很大的局限性 | 不推薦,XML結(jié)構(gòu)的代碼幾乎無法維護 |
OpenOffice | 部署OpenOffice,移植性較差 | - | 需要了解OpenOffice的API |
HTML瀏覽器導(dǎo)出 | 依賴瀏覽器的實現(xiàn),移植性較差 | HTML不能很好的兼容Word的格式,樣式糟糕 | - |
Jacob、winlib | Windows平臺 | - | 復(fù)雜,完全不推薦使用 |
引入依賴
<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.10.0</version> </dependency>
注意,poi-tl是基于poi的,如還引入poi需要注意版本
怎么使用
下面介紹一下poi-tl的幾種常見使用方法
1、文本
標(biāo)簽:{{text}}
數(shù)據(jù)模型:
String
:文本TextRenderData
:有樣式的文本HyperlinkTextRenderData
:超鏈接和錨點文本Object
:調(diào)用 toString() 方法轉(zhuǎn)化為文本
模板:
代碼示例:
public static void main(String[] args) { try { /*文本*/ Map<String, Object> map = new HashMap<>(); map.put("name", "Sayi"); map.put("author", new TextRenderData("000000", "Sayi")); map.put("link", new HyperlinkTextRenderData("website", "http://deepoove.com")); map.put("anchor", new HyperlinkTextRenderData("anchortxt", "anchor:appendix1")); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\1.docx"); } catch (IOException e) { e.printStackTrace(); } } }
效果:
2、圖片
標(biāo)簽:{{@var}}
數(shù)據(jù)模型:
String
:圖片url或者本地路徑,默認(rèn)使用圖片自身尺寸PictureRenderData
ByteArrayPictureRenderData
FilePictureRenderData
UrlPictureRenderData
模板:
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*圖片*/ map.put("image", "D:\\pic.jpg"); // 指定圖片路徑 map.put("svg", "https://profile-avatar.yssmx.com/840f4549a5ae4144b243b203900b98c5_qq_38860250.jpg"); // svg圖片 map.put("image1", Pictures.ofLocal("D:\\pic.jpg").size(120, 120).create()); // 設(shè)置圖片寬高 map.put("streamImg", Pictures.ofStream(new FileInputStream("D:\\pic.jpg"), PictureType.JPEG) // 圖片流 .size(100, 120).create()); map.put("urlImg", Pictures.ofUrl("https://profile-avatar.yssmx.com/840f4549a5ae4144b243b203900b98c5_qq_38860250.jpg") .size(100, 100).create()); // 網(wǎng)絡(luò)圖片(注意網(wǎng)絡(luò)耗時對系統(tǒng)可能的性能影響) XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\2.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
3、表格
標(biāo)簽:{{#tableTest}}
數(shù)據(jù)模型:
TableRenderData
推薦使用工廠 `Tables` 、 `Rows` 和 `Cells` 構(gòu)建表格模型。
模板:
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*表格*/ RowRenderData row0 = Rows.of("姓名", "學(xué)歷").textColor("FFFFFF") .bgColor("4472C4").center().create(); RowRenderData row1 = Rows.create("李四", "博士"); map.put("tableTest", Tables.create(row0, row1)); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\3.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
4、列表
標(biāo)簽:{{*var}}
數(shù)據(jù)模型:
List<String>
NumberingRenderData
推薦使用工廠 `Numberings` 構(gòu)建列表模型。
模板:
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*列表*/ map.put("listTest", Numberings.create("Plug-in grammar", "Supports word text, pictures, table...", "Not just templates")); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\4.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
5、嵌套
標(biāo)簽:{{+nested}}
數(shù)據(jù)模型:
DocxRenderData
推薦使用工廠 `Includes` 構(gòu)建嵌套模型。
模板:
嵌套子模板:
代碼示例:
/** * @Title: PoiTlTest * @Description: * @author: leon * @date: 2023/3/14 19:11 */ public class PoiTlTest { public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*嵌套*/ List<AddrModel> subData = new ArrayList<>(); subData.add(new AddrModel("Guangdong,China")); subData.add(new AddrModel("Shanghai,China")); map.put("nested", Includes.ofLocal("D:\\subInTest.docx").setRenderModel(subData).create()); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\5.docx"); } catch (IOException e) { e.printStackTrace(); } } } class AddrModel { private String addr; public AddrModel(String addr) { this.addr = addr; } // Getter/Setter }
效果:
6、條件判斷顯示
標(biāo)簽 :
{{?condition}} 需要顯示的內(nèi)容 {{/condition}}
模板:
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*條件判斷顯示*/ map.put("condition1", false); //不顯示,默認(rèn)值也為false map.put("condition1Str","顯示1"); map.put("condition2", true); map.put("condition2Str","顯示2"); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\6.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
7、非空集合循環(huán)
標(biāo)簽也為 :
{{?songs}} 需要遍歷的內(nèi)容 {{/songs}}
模板:
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*非空集合循環(huán)*/ Map<String, String> map1 = new HashMap<>(); map1.put("star","周杰倫"); map1.put("song","---蘭亭序"); Map<String, String> map2 = new HashMap<>(); map2.put("star","林俊杰"); map2.put("song","---江南"); map.put("songs", Arrays.asList(map1,map2)); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\7.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
8、圖表
圖標(biāo)有單系列圖表、多系列圖表、組合圖表多種類型,下面只介紹單系列圖表的使用:
標(biāo)簽:{{var}}
注意:該標(biāo)簽不是直接在模板定義,而是在:圖表區(qū)格式—可選文字—標(biāo)題(新版本Microsoft Office標(biāo)簽位置在:編輯替換文字-替換文字)。
下面是Microsoft Office的操作步驟:
點擊插入,選擇相應(yīng)的圖表
編輯替換文字-替換文字
代碼示例:
public static void main(String[] args) { try { Map<String, Object> map = new HashMap<>(); /*圖表*/ //單系列圖表指的是餅圖(3D餅圖)、圓環(huán)圖等。 map.put("chart1",Charts .ofSingleSeries("綜合測評結(jié)果統(tǒng)計", new String[] { "正常", "異常","其他" }) .series("badAndgood", new Integer[] { 19, 17,15}) .create()); XWPFTemplate.compile("D:\\testin.docx").render(map).writeToFile("D:\\8.docx"); } catch (IOException e) { e.printStackTrace(); } }
效果:
文章來源:http://www.zghlxwxcb.cn/news/detail-485055.html
總結(jié)
上面是poi-tl的一些簡單用例,如對其他功能感興趣的朋友,可以去poi-tl的官網(wǎng)學(xué)習(xí),地址為:http://deepoove.com/poi-tl/文章來源地址http://www.zghlxwxcb.cn/news/detail-485055.html
到了這里,關(guān)于Java利用POI-TL模板導(dǎo)出Word文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!