最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件

 更新時(shí)間:2022年08月01日 10:45:52   作者:realpdai???????  
這篇文章主要介紹了SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件,poi-tl是一個(gè)基于Apache?POI的Word模板引擎,也是一個(gè)免費(fèi)開(kāi)源的Java類庫(kù)

前言

poi-tl是一個(gè)基于Apache POI的Word模板引擎,也是一個(gè)免費(fèi)開(kāi)源的Java類庫(kù),你可以非常方便的加入到你的項(xiàng)目中,并且擁有著讓人喜悅的特性。本文主要介紹通過(guò)SpringBoot集成poi-tl實(shí)現(xiàn)模板方式的Word導(dǎo)出功能。

知識(shí)準(zhǔn)備

需要理解文件上傳和下載的常見(jiàn)場(chǎng)景和技術(shù)手段。@pdai

什么是poi-tl

如下內(nèi)容來(lái)源于, ?poi-tl官網(wǎng)??。

poi-tl(poi template language)是Word模板引擎,使用Word模板和數(shù)據(jù)創(chuàng)建很棒的Word文檔。

優(yōu)勢(shì):

它還支持自定義插件,如下是 官網(wǎng)代碼倉(cāng)庫(kù)支持的特性

poi-tl supports custom functions (plug-ins), functions can be executed anywhere in the Word template, do anything anywhere in the document is the goal of poi-tl.

Feature

Description

:white_check_mark: Text

Render the tag as text

:white_check_mark: Picture

Render the tag as a picture

:white_check_mark: Table

Render the tag as a table

:white_check_mark: Numbering

Render the tag as a numbering

:white_check_mark: Chart

Bar chart (3D bar chart), column chart (3D column chart), area chart (3D area chart), line chart (3D line chart), radar chart, pie chart (3D pie Figure) and other chart rendering

:white_check_mark: If Condition

Hide or display certain document content (including text, paragraphs, pictures, tables, lists, charts, etc.) according to conditions

:white_check_mark: Foreach Loop

Loop through certain document content (including text, paragraphs, pictures, tables, lists, charts, etc.) according to the collection

:white_check_mark: Loop table row

Loop to copy a row of the rendered table

:white_check_mark: Loop table column

Loop copy and render a column of the table

:white_check_mark: Loop ordered list

Support the loop of ordered list, and support multi-level list at the same time

:white_check_mark: Highlight code

Word highlighting of code blocks, supporting 26 languages and hundreds of coloring styles

:white_check_mark: Markdown

Convert Markdown to a word document

:white_check_mark: Word attachment

Insert attachment in Word

:white_check_mark: Word Comments

Complete support comment, create comment, modify comment, etc.

:white_check_mark: Word SDT

Complete support structured document tag

:white_check_mark: Textbox

Tag support in text box

:white_check_mark: Picture replacement

Replace the original picture with another picture

:white_check_mark: bookmarks, anchors, hyperlinks

Support setting bookmarks, anchors and hyperlinks in documents

:white_check_mark: Expression Language

Fully supports SpringEL expressions and can extend more expressions: OGNL, MVEL...

:white_check_mark: Style

The template is the style, and the code can also set the style

:white_check_mark: Template nesting

The template contains sub-templates, and the sub-templates then contain sub-templates

:white_check_mark: Merge

Word merge Merge, you can also merge in the specified position

:white_check_mark: custom functions (plug-ins)

Plug-in design, execute function anywhere in the document

poi-tl的TDO模式

TDO模式:Template + data-model = output

以官網(wǎng)的例子為例:

XWPFTemplate template = XWPFTemplate.compile("template.docx").render(
new HashMap<String, Object>(){{
put("title", "Hi, poi-tl Word模板引擎");
}});
template.writeAndClose(new FileOutputStream("output.docx"));
  • compile 編譯模板 - Template
  • render 渲染數(shù)據(jù) - data-model
  • write 輸出到流 - output

Template:模板

模板是Docx格式的Word文檔,你可以使用Microsoft office、WPS Office、Pages等任何你喜歡的軟件制作模板,也可以使用Apache POI代碼來(lái)生成模板。

所有的標(biāo)簽都是以{{開(kāi)頭,以}}結(jié)尾,標(biāo)簽可以出現(xiàn)在任何位置,包括頁(yè)眉,頁(yè)腳,表格內(nèi)部,文本框等,表格布局可以設(shè)計(jì)出很多優(yōu)秀專業(yè)的文檔,推薦使用表格布局。

poi-tl模板遵循“所見(jiàn)即所得”的設(shè)計(jì),模板和標(biāo)簽的樣式會(huì)被完全保留。

Data-model:數(shù)據(jù)

數(shù)據(jù)類似于哈?;蛘咦值?,可以是Map結(jié)構(gòu)(key是標(biāo)簽名稱):

Map<String, Object> data = new HashMap<>();
data.put("name", "Sayi");
data.put("start_time", "2019-08-04");

可以是對(duì)象(屬性名是標(biāo)簽名稱):

public class Data {
private String name;
private String startTime;
private Author author;
}

數(shù)據(jù)可以是樹(shù)結(jié)構(gòu),每級(jí)之間用點(diǎn)來(lái)分隔開(kāi),比如??{ {author.name} }??標(biāo)簽對(duì)應(yīng)的數(shù)據(jù)是author對(duì)象的name屬性值。

Word模板不是由簡(jiǎn)單的文本表示,所以在渲染圖片、表格等元素時(shí)提供了數(shù)據(jù)模型,它們都實(shí)現(xiàn)了接口RenderData,比如圖片數(shù)據(jù)模型PictureRenderData包含圖片路徑、寬、高三個(gè)屬性。

Output:輸出

以流的方式進(jìn)行輸出:

template.write(OutputStream stream);

可以寫(xiě)到任意輸出流中,比如文件流:

template.write(new FileOutputStream("output.docx"));

比如網(wǎng)絡(luò)流:

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=\""+"out_template.docx"+"\"");

// HttpServletResponse response
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out); // 最后不要忘記關(guān)閉這些流。

實(shí)現(xiàn)案例

這里展示SpringBoot集成poi-tl基于word模板導(dǎo)出Word, 以及導(dǎo)出markdown為word的例子。

Pom依賴

引入poi的依賴包

基礎(chǔ)的包:

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

插件的包如下,比如highlight,markdown包

<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl-plugin-highlight</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl-plugin-markdown</artifactId>
<version>1.0.3</version>
</dependency>

導(dǎo)出基于template的word

controller中的方法:

@ApiOperation("Download Word")
@GetMapping("/word/download")
public void download(HttpServletResponse response){
try {
XWPFTemplate document = userService.generateWordXWPFTemplate();
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=user_word_" + System.currentTimeMillis() + ".docx");
OutputStream os = response.getOutputStream();
document.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Service中的實(shí)際方法:

@Override
public XWPFTemplate generateWordXWPFTemplate() throws IOException {
Map<String, Object> content = new HashMap<>();
content.put("title", "Java 全棧知識(shí)體系");
content.put("author", "pdai");
content.put("site", new HyperlinkTextRenderData("https://pdai.tech", "https://pdai.tech"));

content.put("poiText", "Apache POI 是創(chuàng)建和維護(hù)操作各種符合Office Open XML(OOXML)標(biāo)準(zhǔn)和微軟的OLE 2復(fù)合文檔格式(OLE2)的Java API。用它可以使用Java讀取和創(chuàng)建,修改MS Excel文件.而且,還可以使用Java讀取和創(chuàng)建MS Word和MSPowerPoint文件。更多請(qǐng)參考[官方文檔](https://poi.apache.org/index.html)");

content.put("poiText2", "生成xls和xlsx有什么區(qū)別?POI對(duì)Excel中的對(duì)象的封裝對(duì)應(yīng)關(guān)系?");
content.put("poiList", Numberings.create("excel03只能打開(kāi)xls格式,無(wú)法直接打開(kāi)xlsx格式",
"xls只有65536行、256列; xlsx可以有1048576行、16384列",
"xls占用空間大, xlsx占用空間小,運(yùn)算速度也會(huì)快一點(diǎn)"));

RowRenderData headRow = Rows.of("ID", "Name", "Email", "TEL", "Description").textColor("FFFFFF")
.bgColor("4472C4").center().create();
TableRenderData table = Tables.create(headRow);
getUserList()
.forEach(a -> table.addRow(Rows.create(a.getId() + "", a.getUserName(), a.getEmail(), a.getPhoneNumber() + "", a.getDescription())));
content.put("poiTable", table);

Resource resource = new ClassPathResource("pdai-guli.png");
content.put("poiImage", Pictures.ofStream(new FileInputStream(resource.getFile())).create());

return XWPFTemplate.compile(new ClassPathResource("poi-tl-template.docx").getFile()).render(content);
}

private List<User> getUserList(){
List<User> userList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
userList.add(User.builder()
.id(Long.parseLong(i + "")).userName("pdai" + i).email("pdai@pdai.tech" + i).phoneNumber(121231231231L)
.description("hello world" + i)
.build());
}
return userList;
}

準(zhǔn)備模板:

導(dǎo)出word:

導(dǎo)出markdown為word

controller中的方法:

@ApiOperation("Download Word based on markdown")
@GetMapping("/word/downloadMD")
public void downloadMD(HttpServletResponse response){
try {
XWPFTemplate document = userService.generateWordXWPFTemplateMD();
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=user_word_" + System.currentTimeMillis() + ".docx");
OutputStream os = response.getOutputStream();
document.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Service中實(shí)現(xiàn)的方法:

@Override
public XWPFTemplate generateWordXWPFTemplateMD() throws IOException {
MarkdownRenderData code = new MarkdownRenderData();

Resource resource = new ClassPathResource("test.md");
code.setMarkdown(new String(Files.readAllBytes(resource.getFile().toPath())));
code.setStyle(MarkdownStyle.newStyle());

Map<String, Object> data = new HashMap<>();
data.put("md", code);

Configure config = Configure.builder().bind("md", new MarkdownRenderPolicy()).build();

return XWPFTemplate.compile(new ClassPathResource("markdown_template.docx").getFile(), config).render(data);
}

準(zhǔn)備模板:

導(dǎo)出word:

到此這篇關(guān)于SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件的文章就介紹到這了,更多相關(guān)SpringBoot導(dǎo)出Word文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java異常處理的12條軍規(guī)總結(jié)

    Java異常處理的12條軍規(guī)總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java異常處理的12條軍規(guī),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Nacos配置的多文件加載與共享配置方式

    Nacos配置的多文件加載與共享配置方式

    這篇文章主要介紹了Nacos配置的多文件加載與共享配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • java 串口通信實(shí)現(xiàn)流程示例

    java 串口通信實(shí)現(xiàn)流程示例

    這篇文章主要介紹了java 串口通信實(shí)現(xiàn)流程示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • idea多環(huán)境配置及問(wèn)題解決方案

    idea多環(huán)境配置及問(wèn)題解決方案

    這篇文章主要介紹了idea多環(huán)境配置及問(wèn)題解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解

    mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解

    這篇文章主要介紹了mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Eclipse新建web項(xiàng)目流程圖解

    Eclipse新建web項(xiàng)目流程圖解

    這篇文章主要介紹了Eclipse新建web項(xiàng)目流程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • spring 如何解決循環(huán)依賴

    spring 如何解決循環(huán)依賴

    這篇文章主要介紹了spring 如何解決循環(huán)依賴,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-02-02
  • 基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過(guò)程

    基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過(guò)程

    這篇文章主要介紹了基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過(guò)程,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • SpringMVC中如何獲取@PathVariable的值

    SpringMVC中如何獲取@PathVariable的值

    這篇文章主要介紹了SpringMVC中如何獲取@PathVariable的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring Boot支持HTTPS步驟詳解

    Spring Boot支持HTTPS步驟詳解

    這篇文章主要介紹了Spring Boot支持HTTPS步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論

江阴市| 英吉沙县| 桓台县| 汉寿县| 冀州市| 林甸县| 察隅县| 栖霞市| 昌宁县| 汉寿县| 游戏| 南康市| 确山县| 平定县| 洪洞县| 舞阳县| 康马县| 肇州县| 乐平市| 克东县| 错那县| 定州市| 太湖县| 吉水县| 鹤峰县| 孝义市| 永新县| 绍兴县| 玉田县| 镇巴县| 广河县| 刚察县| 民和| 通江县| 即墨市| 莱西市| 邹城市| 田阳县| 南阳市| 东乡县| 平和县|