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對(duì)應(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,可能會(huì)報(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容