freemaker生成word

1.使用word2013或者2007 将要用到的数据格式编写好
后台数据需要的是 Map集合   集合中的key 对应要在
word上显示的值  如 key=name  那么 word上${name}来接收
2.编写完成后,另存为2003.xml的格式
3.将2003.xml 改为2003.ftl 模板生成


工具类 
public class WordExport {
private static Configuration configuration  = new Configuration();
public static void createOutputFile(String path,String moduleName,Map<String,Object> dataMap) {
configuration.setDefaultEncoding("utf-8");
File file = new File(path);
Template t=null;
try {
configuration.setDirectoryForTemplateLoading(file);

            t = configuration.getTemplate("222.ftl");  
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File(path,moduleName+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
} catch (Exception e1) {
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
        
}

  
}

控制层
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String path = request.getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
System.out.println(path+"........");
Map<String , Object> mapData = new HashMap<String, Object>();
  System.out.println(request.getContextPath());
  mapData.put("name", "天津海事局");
  String name=new SimpleDateFormat("yyyyMMddhhmmsss").format(new Date());
       WordExport word = new WordExport();  
       word.createOutputFile(path,name,mapData);  
       File fileInfo  = new File(path+name+".doc");//service.exportDocument("")
     response.setContentType("text/html;charset=utf-8");
     response.setHeader("Content-disposition","attachment;filename="+"xxxx.doc"); 
  InputStream in = new FileInputStream(fileInfo);
            System.out.println(fileInfo.length());
            OutputStream out = response.getOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while((len=in.read(buffer))!=-1)
            out.write(buffer, 0, len);
            in.close();
            out.flush();
  } catch (Exception e) {
  e.printStackTrace();
  }




以下是通用word下载方法
@RequestMapping("dockrepair/reportword")
 public void reportword(HttpServletRequest request,HttpServletResponse response,String id){
  Map<String, Object> reportword = service.reportword(id);
  WordExport we=new WordExport();
  String fname=new SimpleDateFormat("yyyyMMddhhmmsss").format(new Date());
  we.createOutputFile("坞修记录单.ftl", fname, reportword);
  FileDowload f=new FileDowload();
  String path=request.getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
  f.dowload(response,path,fname,"坞修记录单.doc");
 }

public class WordExport {
 private static Configuration configuration  = new Configuration();
 /**
  *
  * @param path        文件路径
  * @param ftlname      模板名称
  * @param fname       文件名称
  * @param dataMap      数据集
  * @return File
  */
 public static void createOutputFile(String ftlname,String fname,Map<String,Object> dataMap) {
  configuration.setDefaultEncoding("utf-8");
  //String path = Local.getRequest().getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
  File file = Local.getProjectSpace().getTemplateSpace();
  Template t=null;
  try {
   configuration.setDirectoryForTemplateLoading(file);
            t = configuration.getTemplate(ftlname); 
  } catch (IOException e) {
   e.printStackTrace();
  }
  //输出文档路径及名称
  File outFile = new File(file.getPath(),fname+".doc");
  Writer out = null;
  try {
   out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
   t.process(dataMap, out);
  } catch (Exception e1) {
   e1.printStackTrace();
  }finally{
   try {
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
       
 } 
}

public class FileDowload {
   public void dowload(HttpServletResponse response,String path,String name,String realname){
   
     
    try {
   response.setContentType("text/html;charset=utf-8");
   response.setHeader("Content-disposition","attachment;filename="+new String(realname.getBytes("gbk"),"iso-8859-1"));
   File file  = new File(path+name+".doc");
      InputStream in = new FileInputStream(file);
      System.out.println(file.length());
      OutputStream out = response.getOutputStream();
      int len;
      byte[] buffer = new byte[1024];
      while((len=in.read(buffer))!=-1)
      out.write(buffer, 0, len);
      in.close();
      out.flush();
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }
 
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值