Java+Freemarker+doc模板+xml转换+list(嵌套)

1.引入pom

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

2.java代码

    @ApiOperation("api接口导出")
    @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创建

image.png

4.xml生成

1.将创建好的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.当前xml使用标签说明

此处dataList对应Java代码中的dataMap.put("dataList", taInterfaceList);
使用#list进行循环
<#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>
记得关闭list标签

嵌套:

此处#list被dataList包含其中,如此嵌套便可,需注意若${ps.name}为null,可能会报错,建议进行判空处理
此处为判空处理示例:<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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容