码迷,mamicode.com
首页 > 其他好文 > 详细

word导出(使用freemarker模板导出)

时间:2016-09-21 23:42:22      阅读:568      评论:0      收藏:0      [点我收藏+]

标签:word导出

package com.shangyukeji.chuangke.controller;


import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.io.Writer;

import java.util.HashMap;

import java.util.Map;

import com.jfinal.plugin.activerecord.Db;

import com.jfinal.plugin.activerecord.Record;



import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import freemarker.template.TemplateException;

import freemarker.template.TemplateExceptionHandler;


public class DocumentHandler {

private Configuration configuration = null;  

private static String filepath = PropKit.use("config.properties").get("file.path"); 

    public DocumentHandler() {  

        configuration = new Configuration();  

        configuration.setDefaultEncoding("utf-8");  

    }  

    public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException {  

    Configuration configuration = new Configuration();  

        Template t=null;  

        try { 

        configuration.setDirectoryForTemplateLoading(new File("D:\\code"));  

           configuration.setDefaultEncoding("utf-8");  

           configuration.setObjectWrapper(new DefaultObjectWrapper());       

             configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);       

             t = configuration.getTemplate("1-1.ftl");  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

        

        File outFile = new File(fileName);  

        Writer out = null;  

        FileOutputStream fos=null;  

        try {  

            fos = new FileOutputStream(outFile);  

            OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");  

             out = new BufferedWriter(oWriter);   

        } catch (FileNotFoundException e1) {  

            e1.printStackTrace();  

        }  

        try {  

            t.process(dataMap, out);  

            out.close();  

            fos.close();  

        } catch (TemplateException e) {  

            e.printStackTrace();  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    } 

    

    public static void downloadWord(String id) throws UnsupportedEncodingException{

    DocumentHandler mdoc = new DocumentHandler();  

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

     dataMap.put("aa", " ");

     dataMap.put("bb", " ");

     dataMap.put("cc", " ");

     dataMap.put("dd", " ");

     dataMap.put("ee", " ");

     dataMap.put("ff", " "); 

     mdoc.createDoc(dataMap, "c:/合同协议.doc");

    }

}

//模板**.xml或者**.ftl都应放在D:\\code文件夹下面(此文件可以随便定义,可以放置任何盘下),生成的文件‘合同协议.doc‘可以放置任何盘下,可以根据个人不同需要随意改动

本文出自 “不凡人生——求知者” 博客,谢绝转载!

word导出(使用freemarker模板导出)

标签:word导出

原文地址:http://825272560.blog.51cto.com/11004487/1854977

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!