① 用FreeMarker方法java生成word文档出现异常
应该是没有成功生成,内部代码里还包含了word无法识别的代码块,所以无法打开,你可以用记事本查看源代码,freenarker转换的word源代码都是xml格式
② freemarker能生成word吗
你说的是freemarker+flyingsaucer+itext用这个生成?
但是我已经用freemark生成了word了,也就是有一个模板了。如果用html生成pdf那就是要再写一个模板。如果少数的几个还可以,但是如果数量较多,比如20、30。就太麻烦了...
③ freemarker 导出word 文档 怎么写
将要导出的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<ChairInfo> list = new ArrayList<ChairInfo>();
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();
}
④ java freemarker引擎 导出word文档
要生成的数据,在java后台必须是用Map格式,这个你检查一下有没有问题。如果有list,也要放到map里面去。
HashMap<String,Object>dataMap=newHashMap<String,Object>();
Configurationconf=newConfiguration();
Templatet=conf.getTemplate(fileName);
FiledocFile=newFile(filePath+"/"+docName);
out=newOutputStreamWriter(newFileOutputStream(docFile),"UTF-8");
t.process(dataMap,out);
⑤ java freemarker将多个word页导出到一个word文件里
你的意思是以一个背景图为主要模板·后面的2 3 4 5...页都要用这个模板 是吧? 那简单·word菜单里有个格式·在格式里有背景-水印-出来一个对话框·图片水印·吧你要的背景放进去 ·就可以了·这样后面的页数都会是这个背景··如果你是插入图片的那个方法··估计你每个页面都要手动去设置了··望采纳··