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

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

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

生成word效果图:

开始

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

2、把word另存为xml文件

3、简化模板内容

格式化XML(在线工具https://www.sojson.com/xml.html

 删除多余标签, 简化<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>、<w:fonts>、<w:styles>、<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>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值