zoukankan      html  css  js  c++  java
  • java使用freemarker生成word文档

    1、原料

      开源jar包freemarker、eclipse、一份模板word文档

    2、首先设计模板word文档

      一般,通过程序输出的word文档的格式是固定的,例如建立一个表格,将表格的标题写好,表格的内容使用不同的标记标好,设计好word后,将word文档另存为xml文件(注:只有word2003 以上的版本可以),使用xml工具打开文件,将要输出到word的内容使用${XX}替换,保存后将文件直接改为tdl后缀

    3、使用eclipse编写程序

       1)获得一个Configuration实例,并为他设置字符集

           configuration = new Configuration();

           configuration.setDefaultEncoding("UTF-8");

       2)解析出word模板

           //获得类解析器,解析地址templatePath,该地址存放模板    我将模板放在该类的包下即/com/welv/this  

           configuration.setClassForTemplateLoading(this.getClass(), templatePath);

           Template t = configuration.getTemplate(templateName);
           t.setEncoding("UTF-8");

      3) 将内容输出到word,并生成文件

          Write out = new OutputStreamWrite(new FileOutputStream("输出的地址"),"utf-8");

          //dataMap为map容器用于存放<输出到word的地点,输出内容>

          t.process(dataMap, out);
          out.close();

          

  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/welv/p/welvOneBlog.html
Copyright © 2011-2022 走看看