Java+Freemarker+doc模板+xml轉(zhuǎn)換+list(嵌套)

1.引入pom

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>

2.java代碼

    @ApiOperation("api接口導(dǎo)出")
    @PostMapping("/apiDoc")
    public void apiDoc(HttpServletResponse response) {
        try {
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("UTF-8");
            //resources
            configuration.setClassForTemplateLoading(getClass(), "/");
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

            // Load the template
            Template template = configuration.getTemplate("b.xml", "UTF-8");
            List<TaInterface> taInterfaceList =taInterfaceService.list();
            dataMap.put("dataList", taInterfaceList);

            // Generate the document content
            StringWriter stringWriter = new StringWriter();
            template.process(dataMap, stringWriter);

            // Set response headers
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=\"apiDoc.doc\"");

            // Write the generated content to response OutputStream
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(stringWriter.toString().getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }

3.docx創(chuàng)建

image.png

4.xml生成

1.將創(chuàng)建好的docx文檔另存為xml


image.png
image.png
image.png

2.打開b.xml文件(打開工具Notepad++)


image.png

3.在線格式化xml:https://www.jyshare.com/front-end/710/

image.png

4.當(dāng)前xml使用標(biāo)簽說明

此處dataList對應(yīng)Java代碼中的dataMap.put("dataList", taInterfaceList);
使用#list進(jìn)行循環(huán)
<#list dataList as data>
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="3"/>
                    <w:rPr>
                        <w:rFonts w:hint="default"/>
                        <w:lang w:val="EN-US" w:fareast="ZH-CN"/>
                    </w:rPr>
                </w:pPr>
                <w:r>
                    <w:rPr>
                        <w:rFonts w:hint="fareast"/>
                        <w:lang w:val="EN-US" w:fareast="ZH-CN"/>
                    </w:rPr>
                    <w:t>${data.name}</w:t>
                </w:r>
            </w:p>
                                ...  ... ... ...
                                ...  ... ... ...
                                ...  ... ... ...
</#list>
記得關(guān)閉list標(biāo)簽

嵌套:

此處#list被dataList包含其中垃帅,如此嵌套便可,需注意若${ps.name}為null邦危,可能會報(bào)錯(cuò)捕犬,建議進(jìn)行判空處理
此處為判空處理示例:<w:t><#if ps.name?exists>${ps.name}<#else>未定義</#if></w:t>

<#list data.taInterfaceParamsList as ps>
                <w:tr>
                    <w:tblPrEx>
                        <w:tblBorders>
                            <w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            <w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            <w:insideH w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            <w:insideV w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                        </w:tblBorders>
                        <w:tblCellMar>
                            <w:top w:w="0" w:type="dxa"/>
                            <w:left w:w="0" w:type="dxa"/>
                            <w:bottom w:w="0" w:type="dxa"/>
                            <w:right w:w="0" w:type="dxa"/>
                        </w:tblCellMar>
                    </w:tblPrEx>
                    <w:trPr>
                        <w:trHeight w:val="480" w:h-rule="atLeast"/>
                        <w:tblCellSpacing w:w="0" w:type="dxa"/>
                    </w:trPr>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="0" w:type="auto"/>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:tcMar>
                                <w:top w:w="0" w:type="dxa"/>
                                <w:left w:w="150" w:type="dxa"/>
                                <w:bottom w:w="0" w:type="dxa"/>
                                <w:right w:w="0" w:type="dxa"/>
                            </w:tcMar>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p>
                            <w:pPr>
                                <w:pStyle w:val="a6"/>
                                <w:keepNext w:val="off"/>
                                <w:keepLines w:val="off"/>
                                <w:pageBreakBefore w:val="off"/>
                                <w:widowControl/>
                                <w:supressLineNumbers w:val="off"/>
                                <w:kinsoku/>
                                <w:wordWrap/>
                                <w:overflowPunct/>
                                <w:topLinePunct w:val="off"/>
                                <w:autoSpaceDE/>
                                <w:autoSpaceDN/>
                                <w:adjustRightInd/>
                                <w:snapToGrid/>
                                <w:spacing w:line="480" w:line-rule="auto"/>
                                <w:textAlignment w:val="auto"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="宋體" w:h-ansi="宋體" w:cs="宋體" w:hint="default"/>
                                    <w:b-cs/>
                                    <w:color w:val="000000"/>
                                    <w:sz-cs w:val="21"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r>
                                <w:rPr>
                                    <w:rFonts w:fareast="宋體" w:cs="宋體" w:hint="fareast"/>
                                    <w:b w:val="off"/>
                                    <w:b-cs/>
                                    <w:color w:val="000000"/>
                                    <w:sz-cs w:val="21"/>
                                    <w:lang w:val="EN-US" w:fareast="ZH-CN"/>
                                </w:rPr>
                                        <w:t>${ps.name}</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>

5.祝君順利,永無bug

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末砖茸,一起剝皮案震驚了整個(gè)濱河市隘擎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凉夯,老刑警劉巖货葬,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異劲够,居然都是意外死亡震桶,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進(jìn)店門征绎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蹲姐,“玉大人,你說我怎么就攤上這事〔穸眨” “怎么了忙厌?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長江咳。 經(jīng)常有香客問我逢净,道長,這世上最難降的妖魔是什么扎阶? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任汹胃,我火速辦了婚禮,結(jié)果婚禮上东臀,老公的妹妹穿的比我還像新娘着饥。我一直安慰自己,他們只是感情好惰赋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布宰掉。 她就那樣靜靜地躺著,像睡著了一般赁濒。 火紅的嫁衣襯著肌膚如雪轨奄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天拒炎,我揣著相機(jī)與錄音挪拟,去河邊找鬼。 笑死击你,一個(gè)胖子當(dāng)著我的面吹牛玉组,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播丁侄,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼惯雳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鸿摇?” 一聲冷哼從身側(cè)響起石景,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎拙吉,沒想到半個(gè)月后潮孽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡庐镐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年恩商,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片必逆。...
    茶點(diǎn)故事閱讀 40,146評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出名眉,到底是詐尸還是另有隱情粟矿,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布损拢,位于F島的核電站陌粹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏福压。R本人自食惡果不足惜掏秩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望荆姆。 院中可真熱鬧蒙幻,春花似錦、人聲如沸胆筒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仆救。三九已至抒和,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間彤蔽,已是汗流浹背摧莽。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留顿痪,地道東北人镊辕。 一個(gè)月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像员魏,于是被迫代替她去往敵國和親丑蛤。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容