MR>CODER

导航

Java使用velocity导出word

效果展示:

         使用word编辑好模板

 

先在模板中填充部分数据(之所以先写好两条数据,是为了在保存为xml用文本编辑器打开后快速定位到需要出传入的数据。增加不同颜色的两条数据,是为了判断颜色),然后保存文件为

然后再用普通的文本编辑器打开

 

以上是模板的创建。

         导出后的word文档展示

 

 

开发过程记录:

1,  首先创建工程,导入

2,  创建模板文件,将修改好的xml文件用普通文本工作打开,然后复制到改文件中。

3,  新建

4,  执行该类,即可导出word。

代码展示:

 

Person.java

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

 

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.VelocityEngine;

 

public class VelocityUtils {

 

  public static void createDoc(VelocityContext vc, String templetePath) throws Exception {

    Properties ps = new Properties();

    ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "./src/");// 这是模板所在路径

    VelocityEngine ve = new VelocityEngine();

    ve.init(ps);

    ve.init();

    Template template = ve.getTemplate(templetePath, "utf-8");

 

    File srcFile = new File("C:/Users/Administrator/Desktop/abc.doc");//输出路径

    FileOutputStream fos = new FileOutputStream(srcFile);

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));

    template.merge(vc, writer);

    writer.flush();

    writer.close();

    fos.close();

  }

 

  public static void main(String[] args) {

    VelocityContext velocityContext = new VelocityContext();

    velocityContext.put("title", "测试");

    List<Person> personMap=new ArrayList<Person>();

    for(int i=0;i<5;i++){

             Person p=new Person();

             p.setId("0000"+String.valueOf(0+i));

             p.setName("liuhanchang"+i);

             p.setSex("男");

             p.setAge(22);

             personMap.add(p);

    }

    velocityContext.put("personMap",personMap);

    try {

      VelocityUtils.createDoc(velocityContext, "velocityMes.vm");

      System.out.println("=====");

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

}
View Code

 

 

VelocityUtils.java

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

 

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.VelocityEngine;

 

public class VelocityUtils {

 

  public static void createDoc(VelocityContext vc, String templetePath) throws Exception {

    Properties ps = new Properties();

    ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "./src/");// 这是模板所在路径

    VelocityEngine ve = new VelocityEngine();

    ve.init(ps);

    ve.init();

    Template template = ve.getTemplate(templetePath, "utf-8");

 

    File srcFile = new File("C:/Users/Administrator/Desktop/abc.doc");//输出路径

    FileOutputStream fos = new FileOutputStream(srcFile);

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));

    template.merge(vc, writer);

    writer.flush();

    writer.close();

    fos.close();

  }

 

  public static void main(String[] args) {

    VelocityContext velocityContext = new VelocityContext();

    velocityContext.put("title", "测试");

    List<Person> personMap=new ArrayList<Person>();

    for(int i=0;i<5;i++){

             Person p=new Person();

             p.setId("0000"+String.valueOf(0+i));

             p.setName("liuhanchang"+i);

             p.setSex("男");

             p.setAge(22);

             personMap.add(p);

    }

    velocityContext.put("personMap",personMap);

    try {

      VelocityUtils.createDoc(velocityContext, "velocityMes.vm");

      System.out.println("=====");

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

}
View Code

模板

 1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 2 
 3 <?mso-application progid="Word.Document"?>
 4 
 5 <w:wordDocument 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:w="http://schemas.microsoft.com/office/word/2003/wordml" 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>xmcx</o:Author><o:LastAuthor>xmcx</o:LastAuthor><o:Revision>2</o:Revision><o:TotalTime>1</o:TotalTime><o:Created>2015-01-15T02:22:00Z</o:Created><o:LastSaved>2015-01-15T02:22:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>8</o:Words><o:Characters>50</o:Characters><o:Lines>1</o:Lines><o:Paragraphs>1</o:Paragraphs><o:CharactersWithSpaces>57</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="00"/><w:family w:val="Roman"/><w:pitch w:val="variable"/><w:sig w:usb-0="E00002FF" w:usb-1="420024FF" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="0000019F" 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="E10002FF" w:usb-1="4000ACFF" w:usb-2="00000009" 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="00947523"/><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="000124FB"/><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="000124FB"/><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="000124FB"/><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="000124FB"/><w:rPr><w:sz w:val="18"/><w:sz-cs w:val="18"/></w:rPr></w:style><w:style w:type="table" w:styleId="a5"><w:name w:val="Table Grid"/><wx:uiName wx:val="网格型"/><w:basedOn w:val="a1"/><w:rsid w:val="000124FB"/><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="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="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:proofState w:spelling="clean" w:grammar="clean"/><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:savePreviewPicture/><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="007960E9" wsp:rsidRDefault="007960E9" wsp:rsidP="000124FB"><w:r><w:separator/></w:r></w:p></w:footnote><w:footnote w:type="continuation-separator"><w:p wsp:rsidR="007960E9" wsp:rsidRDefault="007960E9" wsp:rsidP="000124FB"><w:r><w:continuationSeparator/></w:r></w:p></w:footnote></w:footnotePr><w:endnotePr><w:endnote w:type="separator"><w:p wsp:rsidR="007960E9" wsp:rsidRDefault="007960E9" wsp:rsidP="000124FB"><w:r><w:separator/></w:r></w:p></w:endnote><w:endnote w:type="continuation-separator"><w:p wsp:rsidR="007960E9" wsp:rsidRDefault="007960E9" wsp:rsidP="000124FB"><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="000124FB"/><wsp:rsid wsp:val="000124FB"/><wsp:rsid wsp:val="007960E9"/><wsp:rsid wsp:val="00947523"/><wsp:rsid wsp:val="00A208D8"/><wsp:rsid wsp:val="00C1735F"/><wsp:rsid wsp:val="00C40223"/><wsp:rsid wsp:val="00E31BA5"/><wsp:rsid wsp:val="00E413B2"/><wsp:rsid wsp:val="00E70902"/></wsp:rsids></w:docPr><w:body><w:p wsp:rsidR="000124FB" wsp:rsidRDefault="00E70902"><w:pPr><w:rPr><w:rFonts w:hint="fareast"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/></w:rPr><w:t>$title</w:t></w:r></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="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: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="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidTr="00A208D8"><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="C0504D"/></w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E70902"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>No.1</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="C0504D"/></w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E70902"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>Name</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="C0504D"/></w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E70902"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>Age</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="C0504D"/></w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E31BA5"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>Sex</w:t></w:r></w:p></w:tc></w:tr>
 6 
 7 #foreach($person in $personMap)
 8 
 9  
10 
11 <w:tr wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidTr="00A208D8"><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa"/>
12 
13 #if($velocityCount%2==0)
14 
15 <w:shd w:val="clear" w:color="auto" w:fill="8DB3E2"/>
16 
17 #else
18 
19 <w:shd w:val="clear" w:color="auto" w:fill="74D280"/>
20 
21 #end
22 
23 </w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E31BA5"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>$person.getId()</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2130" w:type="dxa"/>
24 
25 #if($velocityCount%2==0)
26 
27 <w:shd w:val="clear" w:color="auto" w:fill="8DB3E2"/>
28 
29 #else
30 
31 <w:shd w:val="clear" w:color="auto" w:fill="74D280"/>
32 
33 #end
34 
35 </w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E31BA5"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>$person.getName()</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa"/>
36 
37 #if($velocityCount%2==0)
38 
39 <w:shd w:val="clear" w:color="auto" w:fill="8DB3E2"/>
40 
41 #else
42 
43 <w:shd w:val="clear" w:color="auto" w:fill="74D280"/>
44 
45 #end
46 
47 </w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E31BA5"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr><w:t>$person.getAge()</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2131" w:type="dxa"/>
48 
49 #if($velocityCount%2==0)
50 
51 <w:shd w:val="clear" w:color="auto" w:fill="8DB3E2"/>
52 
53 #else
54 
55 <w:shd w:val="clear" w:color="auto" w:fill="74D280"/>
56 
57 #end
58 
59 </w:tcPr><w:p wsp:rsidR="00E70902" wsp:rsidRPr="00A208D8" wsp:rsidRDefault="00E31BA5"><w:pPr><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/></w:rPr></w:pPr><w:r wsp:rsidRPr="00A208D8"><w:rPr><w:rFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" w:hint="fareast"/><wx:font wx:val="宋体"/></w:rPr><w:t>$person.getSex()</w:t></w:r></w:p></w:tc></w:tr>
60 
61 #end
62 
63 </w:tbl><w:p wsp:rsidR="00E70902" wsp:rsidRDefault="00E70902"><w:pPr><w:rPr><w:rFonts w:hint="fareast"/></w:rPr></w:pPr></w:p><w:sectPr wsp:rsidR="00E70902" wsp:rsidSect="00947523"><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>
View Code

 

 

开发要点记录:

1,首先熟悉velocity模板语法,详细参考文档。

2,在隔行换色时,应先在word模板中做好。然后找到

红线标注部分为控制背景颜色的语句。

3, 在Velocity中,有一个变量$velocityCount用作循环计数,初始值是1。这个变量的名字和初始值是在velocity.properties文件里配置的。

posted on 2015-01-23 09:53  MR>CODER  阅读(1925)  评论(0编辑  收藏  举报