乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > freemarker 导出word 文档 怎么写-freemarker生成word,freemarker生成word教程

freemarker 导出word 文档 怎么写-freemarker生成word,freemarker生成word教程

作者:乔山办公网日期:

返回目录:word文档


首先,通过xml模板可以将基本上所有的格式都事先锁定,包括页码和分页,只要你事先预设好就能够通过freemarker实现生成,接下来就是我这个问题了,目录怎么解决,下面是解决思路:1:目录的内容可以根据之前其他的内容一样解决,通过XML模板预先设置好,2:目录的页码已经研究过是不能直接通过xml模板实现动态对应了(至少我没搞定0.0)3:由于不能够一步到位,我采取了在模板中预留了一页空白页,只留了抬头的目录两个字,然后通过查询目录二字进行目录的生成,这个功能也是我刚刚折腾出来的0.0目前还没测试能不能用模板生成目录后再更新目录0.0,不过想想可以直接生成目录应该就不用这么麻烦了,至于word生成后的修改0.0我觉得还是以后再说吧..整体来说应该还算完美解决了,代码我就不在这贴出来了~虽然中间折腾了半天走了半天弯路~讲道理还是用的jacob来实现的。。

将要导出的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);

// 设置模本装置方法和路径e68a847a686964616f333,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();

}
首先,通过xml模板可以将基本上所有的格式都事先锁定,包括页码和分页,只要你事先预设好就能够通过freemarker实现生成,
接下来就是我这个问题了,目录怎么解决,下面是解决思路:
1:目录的内容可以根据之前其他的内容一样解决,通过XML模板预先设置好,
2:目录的页码已经研究过是不能直接通过xml模板实现动态对应了(至少我没搞定0.0)
3:由于不能够一步到位,我采取了在模板中预留了一页空白页,只留了抬头的目录两个字,然后通过查询目录二字进行目录的生成,

这个功能也是我刚刚折腾出来的0.0目前还没测试能不能用模板生成目录后再更新目录0.0,不过想想可以直接生成目录应该就不用这么麻烦了,至于word生成后的修改0.0我觉得还是以后再说吧.....
整体来说应该还算完美解决了,代码我就不在这贴出来了~
虽然中间折腾了半天走了半天弯路~
讲道理还是用的jacob来实现的。。

将要导出e68a847a686964616f333的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();

}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网