freemarker 导出word 文档 怎么写
答案:1 悬赏:80
解决时间 2021-02-04 01:48
- 提问者网友:逝爱
- 2021-02-03 13:22
freemarker 导出word 文档 怎么写
最佳答案
- 二级知识专家网友:如果这是命
- 2021-02-03 14:54
将要导出的Word另存为xml格式的文件,打开xml 在其中添加freemarker标签,然后另存为outChairDoc.ftl文件.
第一步要加入Freemarker.jar包。
Servlet代码如下:在outChairDoc.ftl放在包cn.skyclass.jiaowu.freemarker.template下
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
createDoc(response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createDoc(HttpServletResponse response) throws Exception {
// 要填入模本的数据文件
Map dataMap = new HashMap();
dataMap.put("docTitle", "fdfdfdfdfdfdf用户信息");
List list = new ArrayList();
ChairInfo chairInfo = new ChairInfo();
chairInfo.setChairTitle("dfdfd");
chairInfo.setTeacherName("tea");
chairInfo.setStartTime(new Date());
chairInfo.setPlace("dfdfd");
list.add(chairInfo);
dataMap.put("chairInfoList", list);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/cn/skyclass/jiaowu/freemarker/template");
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("outChairDoc.ftl");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
String fileName="讲座列表.doc";
response.setContentType("application/msword");
response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));//可防止导出的文件乱码
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
t.process(dataMap, out);
out.close();
}
第一步要加入Freemarker.jar包。
Servlet代码如下:在outChairDoc.ftl放在包cn.skyclass.jiaowu.freemarker.template下
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
createDoc(response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createDoc(HttpServletResponse response) throws Exception {
// 要填入模本的数据文件
Map dataMap = new HashMap();
dataMap.put("docTitle", "fdfdfdfdfdfdf用户信息");
List
ChairInfo chairInfo = new ChairInfo();
chairInfo.setChairTitle("dfdfd");
chairInfo.setTeacherName("tea");
chairInfo.setStartTime(new Date());
chairInfo.setPlace("dfdfd");
list.add(chairInfo);
dataMap.put("chairInfoList", list);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/cn/skyclass/jiaowu/freemarker/template");
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("outChairDoc.ftl");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
String fileName="讲座列表.doc";
response.setContentType("application/msword");
response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));//可防止导出的文件乱码
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
t.process(dataMap, out);
out.close();
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯