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'可以放置任何盘下,可以根据个人不同需要随意改动
本文转自 沉淀人生 51CTO博客,原文链接:http://blog.51cto.com/825272560/1854977