JAVA利用FreeMarker生成(导出)Word文档

来源:互联网 发布:mac微信怎么截图 编辑:程序博客网 时间:2024/06/05 03:39
一、首先下载相对应的jar包:freemarker.jar;
而我的项目环境是:maven
在pom.xml中:
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.8</version>
</dependency>
二、自定义Word模板,另存为.XML文件,打开进行相对应的编辑;
Word模板如下图:
另存为.XML文件如下图:
在线格式化工具:http://tool.oschina.net/codeformat/xml/
打开xml文件,搜索start,end;将其改成:${start},${end}
其模板有遍历的内容,所有要对其设置:
搜索:<w:tr
找到第一个,tr的意思不用解释了吧,代表着一行。
这也意味着找到了Table中的第一行,但是需要遍历的不是从第一行开始,而是从第二行。
好的,继续搜索,找到第二个。


</w:tr>  
<#list list2 as test>  
<w:tr wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidTr="00F95F67">


在它的头上加一个<#list 你的集合名称 as xxxx>  
ok,有开头就有结尾,玩过JSP上的EL表达式应该不会感觉很陌生吧。(比如:<w:t>${test.mir}</w:t>等)
搜索 </w:tr>
同样找到第二个,加上</#list>结束符。
好的,这样就没问题了。
</w:tr>
</#list>  
</w:tbl>
注:我的集合名称是:list2(名称可以自定义);我的集合别名是:test(名称也可以自定义,与下面表达式的保持一致)
三、编辑完成后,把文件后缀名改成.ftl格式;


四、部分代码如下;


在table01.ftl文件中:
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>


<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
  <w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
  <o:DocumentProperties>
    <o:Author>Microsoft</o:Author>
    <o:LastAuthor>Microsoft</o:LastAuthor>
    <o:Revision>2</o:Revision>
    <o:TotalTime>1</o:TotalTime>
    <o:Created>2017-05-03T02:03:00Z</o:Created>
    <o:LastSaved>2017-05-03T02:03:00Z</o:LastSaved>
    <o:Pages>1</o:Pages>
    <o:Words>11</o:Words>
    <o:Characters>67</o:Characters>
    <o:Company>CHINA</o:Company>
    <o:Lines>1</o:Lines>
    <o:Paragraphs>1</o:Paragraphs>
    <o:CharactersWithSpaces>77</o:CharactersWithSpaces>
    <o:Version>12</o:Version>
  </o:DocumentProperties>
  <w:fonts>
    <w:defaultFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman"/>
    <w:font w:name="Times New Roman">
      <w:panose-1 w:val="02020603050405020304"/>
      <w:charset w:val="00"/>
      <w:family w:val="Roman"/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="E0002AFF" w:usb-1="C0007841" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="000001FF" w:csb-1="00000000"/>
    </w:font>
    <w:font w:name="宋体">
      <w:altName w:val="SimSun"/>
      <w:panose-1 w:val="02010600030101010101"/>
      <w:charset w:val="86"/>
      <w:family w:val="auto"/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="00000003" w:usb-1="288F0000" w:usb-2="00000016" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/>
    </w:font>
    <w:font w:name="Cambria Math">
      <w:panose-1 w:val="02040503050406030204"/>
      <w:charset w:val="01"/>
      <w:family w:val="Roman"/>
      <w:notTrueType/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="00000000" w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="00000000" w:csb-1="00000000"/>
    </w:font>
    <w:font w:name="Calibri">
      <w:panose-1 w:val="020F0502020204030204"/>
      <w:charset w:val="00"/>
      <w:family w:val="Swiss"/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="E00002FF" w:usb-1="4000ACFF" w:usb-2="00000001" w:usb-3="00000000" w:csb-0="0000019F" w:csb-1="00000000"/>
    </w:font>
    <w:font w:name="@宋体">
      <w:panose-1 w:val="02010600030101010101"/>
      <w:charset w:val="86"/>
      <w:family w:val="auto"/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="00000003" w:usb-1="288F0000" w:usb-2="00000016" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/>
    </w:font>
  </w:fonts>
  <w:styles>
    <w:versionOfBuiltInStylenames w:val="7"/>
    <w:latentStyles w:defLockedState="off" w:latentStyleCount="267">
      <w:lsdException w:name="Normal"/>
      <w:lsdException w:name="heading 1"/>
      <w:lsdException w:name="heading 2"/>
      <w:lsdException w:name="heading 3"/>
      <w:lsdException w:name="heading 4"/>
      <w:lsdException w:name="heading 5"/>
      <w:lsdException w:name="heading 6"/>
      <w:lsdException w:name="heading 7"/>
      <w:lsdException w:name="heading 8"/>
      <w:lsdException w:name="heading 9"/>
      <w:lsdException w:name="toc 1"/>
      <w:lsdException w:name="toc 2"/>
      <w:lsdException w:name="toc 3"/>
      <w:lsdException w:name="toc 4"/>
      <w:lsdException w:name="toc 5"/>
      <w:lsdException w:name="toc 6"/>
      <w:lsdException w:name="toc 7"/>
      <w:lsdException w:name="toc 8"/>
      <w:lsdException w:name="toc 9"/>
      <w:lsdException w:name="caption"/>
      <w:lsdException w:name="Title"/>
      <w:lsdException w:name="Default Paragraph Font"/>
      <w:lsdException w:name="Subtitle"/>
      <w:lsdException w:name="Strong"/>
      <w:lsdException w:name="Emphasis"/>
      <w:lsdException w:name="Table Grid"/>
      <w:lsdException w:name="Placeholder Text"/>
      <w:lsdException w:name="No Spacing"/>
      <w:lsdException w:name="Light Shading"/>
      <w:lsdException w:name="Light List"/>
      <w:lsdException w:name="Light Grid"/>
      <w:lsdException w:name="Medium Shading 1"/>
      <w:lsdException w:name="Medium Shading 2"/>
      <w:lsdException w:name="Medium List 1"/>
      <w:lsdException w:name="Medium List 2"/>
      <w:lsdException w:name="Medium Grid 1"/>
      <w:lsdException w:name="Medium Grid 2"/>
      <w:lsdException w:name="Medium Grid 3"/>
      <w:lsdException w:name="Dark List"/>
      <w:lsdException w:name="Colorful Shading"/>
      <w:lsdException w:name="Colorful List"/>
      <w:lsdException w:name="Colorful Grid"/>
      <w:lsdException w:name="Light Shading Accent 1"/>
      <w:lsdException w:name="Light List Accent 1"/>
      <w:lsdException w:name="Light Grid Accent 1"/>
      <w:lsdException w:name="Medium Shading 1 Accent 1"/>
      <w:lsdException w:name="Medium Shading 2 Accent 1"/>
      <w:lsdException w:name="Medium List 1 Accent 1"/>
      <w:lsdException w:name="Revision"/>
      <w:lsdException w:name="List Paragraph"/>
      <w:lsdException w:name="Quote"/>
      <w:lsdException w:name="Intense Quote"/>
      <w:lsdException w:name="Medium List 2 Accent 1"/>
      <w:lsdException w:name="Medium Grid 1 Accent 1"/>
      <w:lsdException w:name="Medium Grid 2 Accent 1"/>
      <w:lsdException w:name="Medium Grid 3 Accent 1"/>
      <w:lsdException w:name="Dark List Accent 1"/>
      <w:lsdException w:name="Colorful Shading Accent 1"/>
      <w:lsdException w:name="Colorful List Accent 1"/>
      <w:lsdException w:name="Colorful Grid Accent 1"/>
      <w:lsdException w:name="Light Shading Accent 2"/>
      <w:lsdException w:name="Light List Accent 2"/>
      <w:lsdException w:name="Light Grid Accent 2"/>
      <w:lsdException w:name="Medium Shading 1 Accent 2"/>
      <w:lsdException w:name="Medium Shading 2 Accent 2"/>
      <w:lsdException w:name="Medium List 1 Accent 2"/>
      <w:lsdException w:name="Medium List 2 Accent 2"/>
      <w:lsdException w:name="Medium Grid 1 Accent 2"/>
      <w:lsdException w:name="Medium Grid 2 Accent 2"/>
      <w:lsdException w:name="Medium Grid 3 Accent 2"/>
      <w:lsdException w:name="Dark List Accent 2"/>
      <w:lsdException w:name="Colorful Shading Accent 2"/>
      <w:lsdException w:name="Colorful List Accent 2"/>
      <w:lsdException w:name="Colorful Grid Accent 2"/>
      <w:lsdException w:name="Light Shading Accent 3"/>
      <w:lsdException w:name="Light List Accent 3"/>
      <w:lsdException w:name="Light Grid Accent 3"/>
      <w:lsdException w:name="Medium Shading 1 Accent 3"/>
      <w:lsdException w:name="Medium Shading 2 Accent 3"/>
      <w:lsdException w:name="Medium List 1 Accent 3"/>
      <w:lsdException w:name="Medium List 2 Accent 3"/>
      <w:lsdException w:name="Medium Grid 1 Accent 3"/>
      <w:lsdException w:name="Medium Grid 2 Accent 3"/>
      <w:lsdException w:name="Medium Grid 3 Accent 3"/>
      <w:lsdException w:name="Dark List Accent 3"/>
      <w:lsdException w:name="Colorful Shading Accent 3"/>
      <w:lsdException w:name="Colorful List Accent 3"/>
      <w:lsdException w:name="Colorful Grid Accent 3"/>
      <w:lsdException w:name="Light Shading Accent 4"/>
      <w:lsdException w:name="Light List Accent 4"/>
      <w:lsdException w:name="Light Grid Accent 4"/>
      <w:lsdException w:name="Medium Shading 1 Accent 4"/>
      <w:lsdException w:name="Medium Shading 2 Accent 4"/>
      <w:lsdException w:name="Medium List 1 Accent 4"/>
      <w:lsdException w:name="Medium List 2 Accent 4"/>
      <w:lsdException w:name="Medium Grid 1 Accent 4"/>
      <w:lsdException w:name="Medium Grid 2 Accent 4"/>
      <w:lsdException w:name="Medium Grid 3 Accent 4"/>
      <w:lsdException w:name="Dark List Accent 4"/>
      <w:lsdException w:name="Colorful Shading Accent 4"/>
      <w:lsdException w:name="Colorful List Accent 4"/>
      <w:lsdException w:name="Colorful Grid Accent 4"/>
      <w:lsdException w:name="Light Shading Accent 5"/>
      <w:lsdException w:name="Light List Accent 5"/>
      <w:lsdException w:name="Light Grid Accent 5"/>
      <w:lsdException w:name="Medium Shading 1 Accent 5"/>
      <w:lsdException w:name="Medium Shading 2 Accent 5"/>
      <w:lsdException w:name="Medium List 1 Accent 5"/>
      <w:lsdException w:name="Medium List 2 Accent 5"/>
      <w:lsdException w:name="Medium Grid 1 Accent 5"/>
      <w:lsdException w:name="Medium Grid 2 Accent 5"/>
      <w:lsdException w:name="Medium Grid 3 Accent 5"/>
      <w:lsdException w:name="Dark List Accent 5"/>
      <w:lsdException w:name="Colorful Shading Accent 5"/>
      <w:lsdException w:name="Colorful List Accent 5"/>
      <w:lsdException w:name="Colorful Grid Accent 5"/>
      <w:lsdException w:name="Light Shading Accent 6"/>
      <w:lsdException w:name="Light List Accent 6"/>
      <w:lsdException w:name="Light Grid Accent 6"/>
      <w:lsdException w:name="Medium Shading 1 Accent 6"/>
      <w:lsdException w:name="Medium Shading 2 Accent 6"/>
      <w:lsdException w:name="Medium List 1 Accent 6"/>
      <w:lsdException w:name="Medium List 2 Accent 6"/>
      <w:lsdException w:name="Medium Grid 1 Accent 6"/>
      <w:lsdException w:name="Medium Grid 2 Accent 6"/>
      <w:lsdException w:name="Medium Grid 3 Accent 6"/>
      <w:lsdException w:name="Dark List Accent 6"/>
      <w:lsdException w:name="Colorful Shading Accent 6"/>
      <w:lsdException w:name="Colorful List Accent 6"/>
      <w:lsdException w:name="Colorful Grid Accent 6"/>
      <w:lsdException w:name="Subtle Emphasis"/>
      <w:lsdException w:name="Intense Emphasis"/>
      <w:lsdException w:name="Subtle Reference"/>
      <w:lsdException w:name="Intense Reference"/>
      <w:lsdException w:name="Book Title"/>
      <w:lsdException w:name="Bibliography"/>
      <w:lsdException w:name="TOC Heading"/>
    </w:latentStyles>
    <w:style w:type="paragraph" w:default="on" w:styleId="a">
      <w:name w:val="Normal"/>
      <wx:uiName wx:val="正文"/>
      <w:rsid w:val="001C5C68"/>
      <w:pPr>
        <w:widowControl w:val="off"/>
        <w:jc w:val="both"/>
      </w:pPr>
      <w:rPr>
        <wx:font wx:val="Calibri"/>
        <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:style w:type="character" w:default="on" w:styleId="a0">
      <w:name w:val="Default Paragraph Font"/>
      <wx:uiName wx:val="默认段落字体"/>
    </w:style>
    <w:style w:type="table" w:default="on" w:styleId="a1">
      <w:name w:val="Normal Table"/>
      <wx:uiName wx:val="普通表格"/>
      <w:rPr>
        <wx:font wx:val="Calibri"/>
        <w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA"/>
      </w:rPr>
      <w:tblPr>
        <w:tblInd w:w="0" w:type="dxa"/>
        <w:tblCellMar>
          <w:top w:w="0" w:type="dxa"/>
          <w:left w:w="108" w:type="dxa"/>
          <w:bottom w:w="0" w:type="dxa"/>
          <w:right w:w="108" w:type="dxa"/>
        </w:tblCellMar>
      </w:tblPr>
    </w:style>
    <w:style w:type="list" w:default="on" w:styleId="a2">
      <w:name w:val="No List"/>
      <wx:uiName wx:val="无列表"/>
    </w:style>
    <w:style w:type="paragraph" w:styleId="a3">
      <w:name w:val="header"/>
      <wx:uiName wx:val="页眉"/>
      <w:basedOn w:val="a"/>
      <w:link w:val="Char"/>
      <w:rsid w:val="00784607"/>
      <w:pPr>
        <w:pBdr>
          <w:bottom w:val="single" w:sz="6" wx:bdrwidth="15" w:space="1" w:color="auto"/>
        </w:pBdr>
        <w:tabs>
          <w:tab w:val="center" w:pos="4153"/>
          <w:tab w:val="right" w:pos="8306"/>
        </w:tabs>
        <w:snapToGrid w:val="off"/>
        <w:jc w:val="center"/>
      </w:pPr>
      <w:rPr>
        <wx:font wx:val="Calibri"/>
        <w:sz w:val="18"/>
        <w:sz-cs w:val="18"/>
      </w:rPr>
    </w:style>
    <w:style w:type="character" w:styleId="Char">
      <w:name w:val="页眉 Char"/>
      <w:basedOn w:val="a0"/>
      <w:link w:val="a3"/>
      <w:rsid w:val="00784607"/>
      <w:rPr>
        <w:sz w:val="18"/>
        <w:sz-cs w:val="18"/>
      </w:rPr>
    </w:style>
    <w:style w:type="paragraph" w:styleId="a4">
      <w:name w:val="footer"/>
      <wx:uiName wx:val="页脚"/>
      <w:basedOn w:val="a"/>
      <w:link w:val="Char0"/>
      <w:rsid w:val="00784607"/>
      <w:pPr>
        <w:tabs>
          <w:tab w:val="center" w:pos="4153"/>
          <w:tab w:val="right" w:pos="8306"/>
        </w:tabs>
        <w:snapToGrid w:val="off"/>
        <w:jc w:val="left"/>
      </w:pPr>
      <w:rPr>
        <wx:font wx:val="Calibri"/>
        <w:sz w:val="18"/>
        <w:sz-cs w:val="18"/>
      </w:rPr>
    </w:style>
    <w:style w:type="character" w:styleId="Char0">
      <w:name w:val="页脚 Char"/>
      <w:basedOn w:val="a0"/>
      <w:link w:val="a4"/>
      <w:rsid w:val="00784607"/>
      <w:rPr>
        <w:sz w:val="18"/>
        <w:sz-cs w:val="18"/>
      </w:rPr>
    </w:style>
    <w:style w:type="character" w:styleId="a5">
      <w:name w:val="Strong"/>
      <wx:uiName wx:val="要点"/>
      <w:basedOn w:val="a0"/>
      <w:rsid w:val="00784607"/>
      <w:rPr>
        <w:b/>
        <w:b-cs/>
      </w:rPr>
    </w:style>
    <w:style w:type="table" w:styleId="a6">
      <w:name w:val="Table Grid"/>
      <wx:uiName wx:val="网格型"/>
      <w:basedOn w:val="a1"/>
      <w:rsid w:val="00784607"/>
      <w:rPr>
        <wx:font wx:val="Calibri"/>
      </w:rPr>
      <w:tblPr>
        <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="000000"/>
          <w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:insideH w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:insideV w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
        </w:tblBorders>
        <w:tblCellMar>
          <w:top w:w="0" w:type="dxa"/>
          <w:left w:w="108" w:type="dxa"/>
          <w:bottom w:w="0" w:type="dxa"/>
          <w:right w:w="108" w:type="dxa"/>
        </w:tblCellMar>
      </w:tblPr>
    </w:style>
  </w:styles>
  <w:shapeDefaults>
    <o:shapedefaults v:ext="edit" spidmax="5122"/>
    <o:shapelayout v:ext="edit">
      <o:idmap v:ext="edit" data="2"/>
    </o:shapelayout>
  </w:shapeDefaults>
  <w:docPr>
    <w:view w:val="print"/>
    <w:zoom w:percent="100"/>
    <w:doNotEmbedSystemFonts/>
    <w:bordersDontSurroundHeader/>
    <w:bordersDontSurroundFooter/>
    <w:defaultTabStop w:val="420"/>
    <w:drawingGridVerticalSpacing w:val="156"/>
    <w:displayHorizontalDrawingGridEvery w:val="0"/>
    <w:displayVerticalDrawingGridEvery w:val="2"/>
    <w:punctuationKerning/>
    <w:characterSpacingControl w:val="CompressPunctuation"/>
    <w:optimizeForBrowser/>
    <w:validateAgainstSchema/>
    <w:saveInvalidXML w:val="off"/>
    <w:ignoreMixedContent w:val="off"/>
    <w:alwaysShowPlaceholderText w:val="off"/>
    <w:hdrShapeDefaults>
      <o:shapedefaults v:ext="edit" spidmax="5122"/>
    </w:hdrShapeDefaults>
    <w:footnotePr>
      <w:footnote w:type="separator">
        <w:p wsp:rsidR="00D6766A" wsp:rsidRDefault="00D6766A" wsp:rsidP="00784607">
          <w:r>
            <w:separator/>
          </w:r>
        </w:p>
      </w:footnote>
      <w:footnote w:type="continuation-separator">
        <w:p wsp:rsidR="00D6766A" wsp:rsidRDefault="00D6766A" wsp:rsidP="00784607">
          <w:r>
            <w:continuationSeparator/>
          </w:r>
        </w:p>
      </w:footnote>
    </w:footnotePr>
    <w:endnotePr>
      <w:endnote w:type="separator">
        <w:p wsp:rsidR="00D6766A" wsp:rsidRDefault="00D6766A" wsp:rsidP="00784607">
          <w:r>
            <w:separator/>
          </w:r>
        </w:p>
      </w:endnote>
      <w:endnote w:type="continuation-separator">
        <w:p wsp:rsidR="00D6766A" wsp:rsidRDefault="00D6766A" wsp:rsidP="00784607">
          <w:r>
            <w:continuationSeparator/>
          </w:r>
        </w:p>
      </w:endnote>
    </w:endnotePr>
    <w:compat>
      <w:spaceForUL/>
      <w:balanceSingleByteDoubleByteWidth/>
      <w:doNotLeaveBackslashAlone/>
      <w:ulTrailSpace/>
      <w:doNotExpandShiftReturn/>
      <w:adjustLineHeightInTable/>
      <w:breakWrappedTables/>
      <w:snapToGridInCell/>
      <w:wrapTextWithPunct/>
      <w:useAsianBreakRules/>
      <w:dontGrowAutofit/>
      <w:useFELayout/>
    </w:compat>
    <wsp:rsids>
      <wsp:rsidRoot wsp:val="00784607"/>
      <wsp:rsid wsp:val="001C5C68"/>
      <wsp:rsid wsp:val="00676513"/>
      <wsp:rsid wsp:val="00784607"/>
      <wsp:rsid wsp:val="00912558"/>
      <wsp:rsid wsp:val="00D6766A"/>
      <wsp:rsid wsp:val="00E047A6"/>
      <wsp:rsid wsp:val="00F95F67"/>
    </wsp:rsids>
  </w:docPr>
  <w:body>
    <w:p wsp:rsidR="001C5C68" wsp:rsidRDefault="00784607" wsp:rsidP="00784607">
      <w:pPr>
        <w:jc w:val="center"/>
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:hint="fareast"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
      </w:pPr>
      <w:r wsp:rsidRPr="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>表1:</w:t>
      </w:r>
      <w:r wsp:rsidR="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:hint="fareast"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>${start}</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>时</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:hint="fareast"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>--</w:t>
      </w:r>
      <w:r wsp:rsidR="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:hint="fareast"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>${end}</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00E047A6">
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
        <w:t>时各区降雨量分布(单位:毫米)</w:t>
      </w:r>
    </w:p>
    <w:p wsp:rsidR="00E047A6" wsp:rsidRPr="00E047A6" wsp:rsidRDefault="00E047A6" wsp:rsidP="00784607">
      <w:pPr>
        <w:jc w:val="center"/>
        <w:rPr>
          <w:rStyle w:val="a5"/>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体"/>
          <wx:font wx:val="宋体"/>
          <w:sz w:val="24"/>
          <w:sz-cs w:val="24"/>
        </w:rPr>
      </w:pPr>
    </w:p>
    <w:tbl>
      <w:tblPr>
        <w:tblW w:w="0" w:type="auto"/>
        <w:tblBorders>
          <w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:insideH w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
          <w:insideV w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
        </w:tblBorders>
        <w:tblLook w:val="04A0"/>
      </w:tblPr>
      <w:tblGrid>
        <w:gridCol w:w="2130"/>
        <w:gridCol w:w="2130"/>
        <w:gridCol w:w="2131"/>
        <w:gridCol w:w="2131"/>
      </w:tblGrid>
      <w:tr wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidTr="00F95F67">
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2130" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00784607" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>区域</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2130" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00784607" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>平均雨量</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2131" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00784607" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>最大累积雨量</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2131" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00784607" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>最大小时滑动雨量</w:t>
            </w:r>
          </w:p>
        </w:tc>
      </w:tr>
 
 
 <#list list2 as test>
 
      <w:tr wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidTr="00F95F67">
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2130" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00912558" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>${test.area}</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2130" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00912558" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>${test.rain}</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2131" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00912558" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>${test.mrain}</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2131" w:type="dxa"/>
          </w:tcPr>
          <w:p wsp:rsidR="00784607" wsp:rsidRPr="00F95F67" wsp:rsidRDefault="00912558" wsp:rsidP="00F95F67">
            <w:pPr>
              <w:jc w:val="center"/>
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
            </w:pPr>
            <w:r wsp:rsidRPr="00F95F67">
              <w:rPr>
                <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体" w:cs="Times New Roman" w:hint="fareast"/>
                <wx:font wx:val="宋体"/>
                <w:sz-cs w:val="21"/>
              </w:rPr>
              <w:t>${test.mir}</w:t>
            </w:r>
          </w:p>
        </w:tc>
      </w:tr>
 
 
 </#list>
 
    </w:tbl>
    <w:p wsp:rsidR="00784607" wsp:rsidRPr="00784607" wsp:rsidRDefault="00784607" wsp:rsidP="00784607">
      <w:pPr>
        <w:jc w:val="left"/>
        <w:rPr>
          <w:rFonts w:ascii="宋体" w:fareast="宋体" w:h-ansi="宋体"/>
          <wx:font wx:val="宋体"/>
          <w:sz-cs w:val="21"/>
        </w:rPr>
      </w:pPr>
    </w:p>
    <w:sectPr wsp:rsidR="00784607" wsp:rsidRPr="00784607" wsp:rsidSect="001C5C68">
      <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="425"/>
      <w:docGrid w:type="lines" w:line-pitch="312"/>
    </w:sectPr>
  </w:body>
</w:wordDocument>




在yuqing2.jsp中:(部分代码)


<!-- 2017.05.02 添加 -->
<input id="day" hidden="true"  value="${day }"/>
<input id="end" hidden="true"  value="${end }"/>
<a href="javascript:void(0)" style="width: 120px;height: 22px;" class="easyui-linkbutton"  onclick="table01()">导出到Word</a><br/><br/>
<!-- 2017.05.02 结束 -->


在export.js中:
function table01(){
var stime = $('#day').val();
var etime = $('#end').val();
$.ajax({
type:"POST",
url:path+"/yuqing/table01",
data:{
stime:stime,
etime : etime
},
cache:false,
dataType:"json",
async:false,
success:function(data){
if(data==1){
alert("导出成功!文件存放到桌面!");
}
else{
alert("导出失败!");
}
}
});
}


在YuqingController.java中:(部分代码)
@RequestMapping(value ="/table06")
public @ResponseBody String table06(HttpServletRequest request,HttpServletResponse response,Model model){
//导出代码:开始
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8"); //设置编码
Map<String,Object> dataMap=new HashMap<String,Object>();
dataMap.put("start", stime); //开始日期
dataMap.put("end", etime); //结束日期

List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>();
for(int i=0;i<list.size();i++) { //遍历结果
Map<String,Object> map = new HashMap<String,Object>();
map.put("area", list.get(i).getArea().toString());
map.put("rain",list.get(i).getRain().toString());
map.put("mrain", list.get(i).getMrain().toString());
map.put("mir",list.get(i).getMir().toString());

list2.add(map);
}
dataMap.put("list2", list2);
configuration.setDirectoryForTemplateLoading(new File("F:\\4\\")); //绝对路径
Template t2=null;
try {
t2 = configuration.getTemplate("table01.ftl"); //ftl文件名
} catch (IOException e) {
e.printStackTrace();
}


File outFile = new File("C:/Users/Administrator/Desktop/word"+Math.random()*10000+".doc"); //桌面路径
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));

} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
 
try {
t2.process(dataMap, out);
out.close();  //输出流一定要关闭;否则,生成的word文档打不开
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//导出代码:结束
return "1";
}


五、导出结果如下图:
六、参考代码如下:


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class WordTest {

private Configuration configuration = null;

public WordTest(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}

public static void main(String[] args) {
WordTest test = new WordTest();
test.createWord();
}

public void createWord(){
Map<String,Object> dataMap=new HashMap<String,Object>();
getData(dataMap);
configuration.setClassForTemplateLoading(this.getClass(), "/com");  //FTL文件所存在的位置
//configuration.setDirectoryForTemplateLoading(new File("F:\\4\\")); //绝对路径
Template t=null;
try {
t = configuration.getTemplate("wordModel.ftl"); //文件名
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("O:/outFilessa"+Math.random()*10000+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
 
        try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


private void getData(Map<String, Object> dataMap) {
dataMap.put("title", "标题");
dataMap.put("year", "2017");
dataMap.put("month", "05");
dataMap.put("day", "03");
dataMap.put("auditor", "XXX");
dataMap.put("phone", "XXXXXXXXXX");
dataMap.put("weave", "XXX");

List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < 10; i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("number", i);
map.put("content", "内容"+i);
list.add(map);
}

dataMap.put("list", list);
}

}




0 0
原创粉丝点击