用freemarker生成word文档

来源:互联网 发布:云通信电话软件 编辑:程序博客网 时间:2024/04/28 10:45

一、先在word中画好需要的表格并定义好参数名:


二、将word文档另存为word 2003xml格式文件。


三、将xml文件后缀名,改为ftl格式,用foxe编辑器打开ftl格式文档,并缩进排版调整格式,替换参数。


循环输出的参数,采用list输出


四、若在文档中需要插入不同的图片,则需要修改文档,src和name不修改将重复显示第一张图片:



五、生成文档所需的java类

package aacmm.module.qm.printfWord;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;


public class DocUtil {
public Configuration configure=null;
   
   public DocUtil(){
       configure=new Configuration();
       configure.setDefaultEncoding("UTF-8");
   }
   /**
    * 根据Doc模板生成word文件
    * @param dataMap 需要填入模板的数据
    * @param downloadType 文件名称
    * @param wordName 生成word文档名称
    * @param res HttpServletResponse
    * @throws IOException 
    */
   public void createDoc(Map<String,Object> dataMap,String downloadType,String wordName,HttpServletResponse res) throws IOException{
    Writer out=null;
    InputStream fin = null;
    ServletOutputStream servletlout = null;
    String file= downloadType + (int)(Math.random() * 100000) + ".doc";
       File outFile=new File(file);
       try {
           //加载需要装填的模板
           Template template=null;
           //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。
           configure.setClassForTemplateLoading(this.getClass(), "ftl");
           //设置对象包装器
           configure.setObjectWrapper(new DefaultObjectWrapper());
           //设置异常处理器
           configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
           //定义Template对象,注意模板类型名字与downloadType要一致
           template=configure.getTemplate(downloadType+".ftl","UTF-8");
         
           out= new OutputStreamWriter(new FileOutputStream(outFile), "utf-8");
           template.process(dataMap, out);
                                            
           outFile.setReadOnly();
           outFile.setWritable(false);
           
           // 调用工具类WordGenerator的createDoc方法生成Word文档  
           fin = new FileInputStream(outFile);
           res.setCharacterEncoding("utf-8");
           res.setContentType("application/msword");
           // 设置浏览器以下载的方式处理该文件默认名为resume.doc
           res.addHeader("Content-Disposition", "attachment;filename=" + new String(wordName.getBytes("gb2312"), "ISO8859-1")  + ".doc");
           servletlout = res.getOutputStream();
           byte[] buffer = new byte[512]; // 缓冲区  
           int bytesToRead = -1;
           // 通过循环将读入的Word文件的内容输出到浏览器中  
           while ((bytesToRead = fin.read(buffer)) != -1) {
            servletlout.write(buffer, 0, bytesToRead);
           }
           out.flush();
           out.close();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (TemplateException e) {
           e.printStackTrace();
       }finally {
            if (servletlout != null) {
            servletlout.close();
            }
            if (fin != null) {
                fin.close();
            }
            if (out != null) {
                out.close();
            }
            if (outFile != null) {
            outFile.delete(); // 删除临时文件  
            }
       }
   }
   
   public String getImageStr(String imgFile) throws IOException{
       InputStream in=null;
       byte[] data=null;
       try {
           in=new FileInputStream(imgFile);
           data=new byte[in.available()];
           in.read(data);
           in.close();
          
       }catch (IOException e) {
          throw e;
       }finally{
        if (in != null) {
           in.close();
        }
       }
       return Base64.encode(data);
   }
}


原创粉丝点击