使用FreeMarker生成word文档

来源:互联网 发布:手机音乐剪切软件 编辑:程序博客网 时间:2024/04/29 05:20

       最近再做一个项目需要生成word文档,通过在网上收集了一些资料后决定采用Freemarker 来生成word文档。其基本过程如下所示:

      1.编辑文档模板

           

           

         该文档的XML格式(格式控制部分省略)如下所示,其中加粗部分是添加的FreeMarker标记。

<w:t>测试文档</w:t>

…………………………

<w:t>作者</w:t></w:r><w:r wsp:rsidR="00E05F5B"><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>${author}</w:t></w:r></w:p>

…………………………

<w:t>日期</w:t></w:r><w:r wsp:rsidR="00E05F5B"><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>${date}</w:t></w:r></w:p>

…………………………

<w:t>版本</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/>

</w:rPr><w:t>:${version}</w:t></w:r></w:p>

…………………………

<w:t>控件信息表</w:t></w:r></w:p>

…………………………

<w:t>编号</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>名称</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>位置</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>长度</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>宽度</w:t></w:r></w:p></w:tc><w:tc><w:tcPr

…………………………

<w:t>说明</w:t></w:r></w:p></w:tc></w:tr>

<#list tabel as list>

…………………………

<w:t>${list.id}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>${list.text}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>${list.location}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>${list.length}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>${list.width}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>

…………………………

<w:t>${list.decration}</w:t></w:r></w:p></w:tc></w:tr>

</#list></w:tbl>

…………………………

<w:t>界面截图:</w:t></w:r></w:p>

…………………………

<w:binDataw:name="wordml://02000001.jpg"xml:space="preserve">${image}</w:binData>

…………………………

</w:sectPr></wx:sect></w:body>

2)将加入FreeMarker标记的文件保存为FTL格式,作为文档模板。编写程序向模板中写入数据;

/**

*通过使用该Javabean,可以向表单中循环输入数据;

*Javabean中主要对表单的属性信息进行控制。

**/

public class tabel {

   private  int id;

   private String text;

   private String location;

   private int length;

   private int width;

   private String decration;

    public String getDecration() {

        return decration;

    }

    public void setDecration(String decration) {

        this.decration = decration;

    }

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public int getLength() {

        return length;

    }

    public void setLength(int length) {

        this.length = length;

    }

    public String getLocation() {

        return location.toString();

    }

    public void setLocation(String str) {

        this.location =str;

    }

      public String getText() {

        return text;

    }

    public void setText(String text) {

        this.text = text;

    }

    public int getWidth() {

        return width;

    }

    public void setWidth(int width) {

        this.width = width;

  }

}

/**

*向模板中诸如数据,生成说明文档;

***/

public class WordTest {

    private Configuration config = null;

    public WordTest() {

        config = new Configuration();

        config.setDefaultEncoding("UTF-8");

}

 

    public void createWord() {

Map<String, Object> dataMap = new HashMap<String, Object>();

getData(dataMap);

//此处获取模板信息

config.setClassForTemplateLoading(this.getClass(), "/org/cs/getword/template");

        Template t = null;

        try {

            t = config.getTemplate("test.ftl");

        } catch (IOException e) {

            e.printStackTrace();

        }

        //生成的说明文档存放路径

        File output = new File("D:/temp/output.doc");

        Writer out = null;

        try {

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));

        } catch (FileNotFoundException e) {

            e.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("author", "陈松");

        dataMap.put("date", "2015.3.2");

        dataMap.put("version", "version1.0");

        List<tabel> _table=new ArrayList<tabel>();

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

        tabel t=new tabel();

        t.setId(1+i);

        t.setText("提交");

        t.setLocation("(100,10)");

        t.setLength(10+i);

        t.setWidth(5+i);

        t.setDecration("单击此按钮提交信息!");

        _table.add(t);

        }

        dataMap.put("tabel", _table);

        String imageFile="D:/temp/test.jpg";

        InputStream in=null;

        byte[] data=null;

        try{

            in=new FileInputStream(imageFile);

            data=new byte[in.available()];

            in.read(data);

            in.close();

        }catch(FileNotFoundException e){

           e.printStackTrace();

        }catch(IOException e){

            e.printStackTrace();

        }

       //对图像数据进行Base64编码。

        BASE64Encoder encoder=new BASE64Encoder();

       dataMap.put("image",encoder.encode(data));

    }

}

所得结果为:






















3.所生成的文档。

0 0
原创粉丝点击