FreeMarker给word文档里生成多个柱状图(数据可修改)

项目场景:

上个博客生成了一个静态的柱状图 也就是将柱状图的图片放到word里,这个博客说说 ftl文件里给他设置数据动态


1创建.docx文档模板:

类似于这样的模板

 


2.生成xml在线解析格式然后转ftl:

类似于这样

它里面会有这样几个标签

<Relationship Id="rId12" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="charts/chart1.xml"/>

<Relationship Id="rId13" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item2.xml"/>

这两个标签的id 和 Target的值都是可以改变的 ,然后找到它Target 对应的xml文件

比如这样

 

这两个name值也都是可以变的 当然 对应着这个xml的标签也要变

/word/charts/_rels/chart1.xml.rels 它里面放的应该是柱状图的结构  
就是这三个标签对应的Target 不要删除
<Relationship Id="rId16" Type="http://schemas.microsoft.com/office/2011/relationships/chartColorStyle"         Target="colors1.xml"/>
<Relationship Id="rId15" Type="http://schemas.microsoft.com/office/2011/relationships/chartStyle" Target="style1.xml"/>
<Relationship Id="rId14" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/package" Target="../embeddings/Workbook1.xlsx"/>
/word/charts/chart1.xml 它里面放的应该是 柱状图的的数据 就是那根柱子的高低

它里面的标签就是柱状图的数据 例如下图

 

 

改动这些数据就可以改动柱状图的数据了

接下来就是去找柱状图放的位置了

<w:body>这个标签里 去找
<w:p>
    <w:pPr>
        <w:pStyle w:val="2"/>
        <w:numPr>
            <w:ilvl w:val="0"/>
            <w:numId w:val="2"/>
        </w:numPr>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
            <w:lang w:val="en-US" w:eastAsia="zh-CN"/>
        </w:rPr>
        <w:t>柱状图lwh</w:t>
    </w:r>
</w:p>
<w:p>
    <w:bookmarkStart w:id="0" w:name="_GoBack"/>
    <w:r>
        <w:drawing>
            <wp:inline distT="0" distB="0" distL="114300" distR="114300">
                <wp:extent cx="5080000" cy="3810000"/>
                <wp:effectExtent l="4445" t="4445" r="20955" b="14605"/>
                <wp:docPr id="1" name="图表 1"/>
                <wp:cNvGraphicFramePr/>
                <a:graphic
                        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                    <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
                        <c:chart
                                xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
                                xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId12"/>
                    </a:graphicData>
                </a:graphic>
            </wp:inline>
        </w:drawing>
    </w:r>
    <w:bookmarkEnd w:id="0"/>
</w:p>

这里就是放柱状图的位置 它里面会引用上面的pkg标签 也就是柱状图的xml 例如下图

一定要对应最上面那个pkg的id

这样 ftl就改好了

 

传多个柱状图:

多个柱状图就写个list在那个标签上让他循环 循环的时候要给他唯一标识 否则柱状图都一样

例如下图

 

然后就是循环柱状图里的数据

 

如果对您有帮助 请 点赞 转发 ,谢谢 最后生成 就这样