Examination_system/Examination_system-1/.svn/pristine/f1/f1d73d6f4955eb87dd37ff0d9c6...

71 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.bonus.core;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public File createDoc(Map<String, Object> dataMap, String fileName, int i) throws UnsupportedEncodingException {
// dataMap 要填入模本的数据文件
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servletclasspath数据库装载
// 这里我们的模板是放在template包下面
configuration.setClassForTemplateLoading(this.getClass(), "/com/bonus/score/controller/template");
Template t = null;
try {
// test.ftl为要装载的模板
//t = configuration.getTemplate("555.ftl");
t = configuration.getTemplate("newtest.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");
// 这个地方对流的编码不可或缺使用main单独调用时应该可以但是如果是web请求导出时导出后word文档就会打不开并且包XML文件错误。主要是编码格式不正确无法解析。
// out = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(outFile)));
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();
}
return outFile;
}
}