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>