FreeMarker生成word-定义模板步骤(带表格且含合并单元格)

本文介绍使用freeMark生成包含表格合并单元的word如何制作模板, 合并单元格主要使用

<w:vmerge w:val='restart'/>和<w:vmerge/>实现。

生成word效果图:

开始

1、编辑word文档,删除重复部分,只保留关键元素

2、把word另存为xml文件

3、简化模板内容

格式化XML(在线工具XML压缩、XML格式化—在线工具

 删除多余标签, 简化<w:wordDocument>标签为:

<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
                xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
                xml:space="preserve">

 简化<o:DocumentProperties>标签为: 

    <o:DocumentProperties>
        <o:Version>16</o:Version>
    </o:DocumentProperties>

<w:fonts> 保留 <w:defaultFonts>子标签

   <w:fonts>
        <w:defaultFonts w:ascii="等线" w:fareast="等线" w:h-ansi="等线" w:cs="Times New Roman"/>
    </w:fonts>

<w:styles> 保留默认段落样式a

   <w:styles>
        <w:versionOfBuiltInStylenames w:val="7"/>
        <w:latentStyles w:defLockedState="off" w:latentStyleCount="376">
        </w:latentStyles>
        <w:style w:type="paragraph" w:default="on" w:styleId="a">
            <w:name w:val="Normal"/>
            <wx:uiName wx:val="正文"/>
            <w:pPr>
                <w:widowControl w:val="off"/>
                <w:jc w:val="both"/>
            </w:pPr>
            <w:rPr>
                <w:rFonts w:ascii="等线" w:fareast="等线" w:h-ansi="等线" w:cs="宋体"/>
                <wx:font wx:val="等线"/>
                <w:kern w:val="2"/>
                <w:sz w:val="21"/>
                <w:sz-cs w:val="22"/>
                <w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA"/>
            </w:rPr>
        </w:style>
    </w:styles>

删除 <o:DocumentProperties>、<w:shapeDefaults>、<w:docPr>

删除 wsp:相关属性(替换为空)

  把段落、文本、单元格样式抽离成 freemark macro

 <#macro pPrSetting>
        <w:pPr>
            <w:pPr>
                <w:widowControl/>
                <w:jc w:val="center"/>
                <w:textAlignment w:val="center"/>
            </w:pPr>
            <#nested>
        </w:pPr>
    </#macro>
    <#macro rPrSetting font="宋体" size="18" bold="false">
        <w:rPr>
            <w:rFonts w:ascii="${font}" w:fareast="${font}" w:cs="${font}" w:h-ansi="${font}" w:hint="fareast"/>
            <#if bold=="true">
                <w:b/>
            </#if>
            <wx:font wx:val="${font}"/>
            <#nested>
            <w:sz w:val="${size}"/>
            <w:sz-cs w:val="${size}"/>
        </w:rPr>
    </#macro>
    <#macro tcPrSetting vmerge="none">
        <w:tcPr>
            <#if vmerge=="restart">
                <w:vmerge w:val="restart"/>
            </#if>
            <#if vmerge=="end">
                <w:vmerge/>
            </#if>
            <#nested>
            <w:vAlign w:val="center"/>
        </w:tcPr>
    </#macro>

4、循环行,处理合并单元格,填充数据

注:需合并的单元格第一行标注 "<w:vmerge w:val="restart" />",从第二行开始,所有要合并的单元标注"<w:vmerge />"

 <#assign count = 0>
    <#assign vmerge = "none">
    <#setting number_format=",##0.0">
    <#assign total = 0>
    <w:body>
        <wx:sect>
            <w:p>
                <w:pPr>
                    <w:spacing w:line="400" w:line-rule="exact"/>
                    <w:jc w:val="left"/>
                </w:pPr>
                <w:r>
                    <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                    <w:t>附件4:</w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:pPr>
                    <w:spacing w:line="580" w:line-rule="exact"/>
                    <w:jc w:val="center"/>
                </w:pPr>
                <w:r>
                    <@rPrSetting font="宋体" size="32" bold="true"></@rPrSetting>
                    <w:t>表1:${name}</w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:pPr>
                    <w:spacing w:line="360" w:line-rule="exact"/>
                    <w:ind w:first-line-chars="550" w:first-line="1320"/>
                </w:pPr>
                <w:r>
                    <@rPrSetting font="方正小标宋简体" size="24"></@rPrSetting>
                    <w:t></w:t>
                </w:r>
                <w:r>
                    <@rPrSetting font="方正小标宋简体" size="24"></@rPrSetting>
                    <w:t></w:t>
                </w:r>
                <w:r>
                    <@rPrSetting font="方正小标宋简体" size="24"></@rPrSetting>
                    <w:t>金额单位:亿元</w:t>
                </w:r>
            </w:p>
            <w:tbl>
                <w:tblPr>
                    <w:tblW w:w="0" w:type="auto"/>
                    <w:tblInd w:w="0" w:type="dxa"/>
                    <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:tblLayout w:type="Fixed"/>
                    <w:tblCellMar>
                        <w:top w:w="15" w:type="dxa"/>
                        <w:left w:w="15" w:type="dxa"/>
                        <w:bottom w:w="15" w:type="dxa"/>
                        <w:right w:w="15" w:type="dxa"/>
                    </w:tblCellMar>
                </w:tblPr>
                <w:tblGrid>
                    <w:gridCol w:w="544"/>
                    <w:gridCol w:w="2287"/>
                    <w:gridCol w:w="1065"/>
                    <w:gridCol w:w="1136"/>
                    <w:gridCol w:w="1136"/>
                    <w:gridCol w:w="1136"/>
                    <w:gridCol w:w="1122"/>
                </w:tblGrid>
                <w:tr>
                    <w:trPr>
                        <w:trHeight w:val="271"/>
                    </w:trPr>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>产业</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>所属行业</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>本年累计</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>占比(%)</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>同比增减额</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>同比增速(%)</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <@tcPrSetting>
                            <w:shd w:val="clear" w:color="auto" w:fill="C0C0C0"/>
                        </@tcPrSetting>
                        <w:p>
                            <@pPrSetting></@pPrSetting>
                            <w:r>
                                <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                <w:t>环比增速(%)</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
                <#list list as item>
                    <#if item.type==0 && item.category==0>
                        <#list item.contents as content>
                            <#assign total = content.value>
                            <w:tr>
                                <w:trPr>
                                    <w:trHeight w:val="301"/>
                                </w:trPr>
                                <w:tc>
                                    <w:tcPr>
                                        <w:tcW w:w="2831" w:type="dxa"/>
                                        <w:gridSpan w:val="2"/>
                                        <w:vAlign w:val="center"/>
                                    </w:tcPr>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>合计</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>${content.value!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>100.0</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>${content.value - content.lastValue}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>${content.yoy!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="true"></@rPrSetting>
                                            <w:t>${content.mom!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                            </w:tr>
                        </#list>
                    </#if>
                </#list>
                <#list list as item>
                    <#if item?has_next>
                        <#if count==0 && item.type==list[item?index+1].type>
                            <#assign vmerge = "restart">
                            <#assign count = count+1>
                        <#elseif count gt 0>
                            <#assign vmerge = "end">
                            <#if item.type!=list[item?index+1].type>
                                <#assign count = 0>
                            </#if>
                        </#if>
                    <#else>
                        <#if count gt 0>
                            <#assign vmerge = "end">
                        </#if>
                    </#if>
                    <#if item.type gte 6 && item.type lte 8>
                        <#list item.contents as content>
                            <w:tr>
                                <w:trPr/>
                                <w:tc>
                                    <@tcPrSetting vmerge="${vmerge}"></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${item.typeName!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="22" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${item.name!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${content.value!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${content.value*100/total!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${content.value - content.lastValue}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${content.yoy!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                                <w:tc>
                                    <@tcPrSetting></@tcPrSetting>
                                    <w:p>
                                        <@pPrSetting></@pPrSetting>
                                        <w:r>
                                            <@rPrSetting font="宋体" size="21" bold="${(item.category==0)? then('true', 'false')}"></@rPrSetting>
                                            <w:t>${content.mom!}</w:t>
                                        </w:r>
                                    </w:p>
                                </w:tc>
                            </w:tr>
                        </#list>
                    </#if>
                </#list>
            </w:tbl>
            <w:p/>
            <w:p/>
            <w:p/>
            <w:p/>
            <w:p/>
            <w:p/>
            <w:p/>
            <w:sectPr>
                <w:pgSz w:w="11906" w:h="16838"/>
                <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="851" w:footer="992"
                         w:gutter="0"/>
                <w:cols w:space="720"/>
                <w:docGrid w:type="lines" w:line-pitch="312"/>
            </w:sectPr>
        </wx:sect>
    </w:body>

SpringBoot_Freemarker生成Word_多个表格+两层嵌套循环; 步骤说明: 1.用Microsoft Office Word打开word原件;将文档中需要动态生成的内容,替换为属性名 ${name} 2.另存为,选择保存类型Word 2003 XML 文档(*.xml) 3.用Firstobject free XML editor打开文件,选择Tools下的Indent【或者按快捷键F8】式化文件内容。左边是文档结构,右边是文档内容; 4. 文档生成后有时需要手动修改,查找第一步中设置的属性名,可能会产生类似${n.....ame}类似的样子,我们将将名字中间的标签删掉,恢复为${name} 5. word模板中有表格,需要循环的位置, 用 标签将第二对 标签(即除表头的w:tr标签后的一对)包围起来 同时表格内的属性例如${name},在这里需要修改为${user.name} (userList是集合在dataMap中的key, user是集合中的每个元素, 类似), 如图: PLUS:若表格之外还有嵌套的循环,也需要用,注意这里的标签不要和某对其他标签交叉,不可以出现这种 6. 标识替换完之后,另存为.ftl后缀文件即可。 代码里是相对有一丢丢复杂的,两层嵌套循环; 总(dataMap) deptName 部门名 list(Table)表的集合 table1(map) table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table2 table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table3 ……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值