欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

freemarker根据模板生成word文件实现导出功能

程序员文章站 2022-05-25 16:58:33
一、准备工作 1.创建一个03的word文档,动态的数据用占位符标志占位(如testname)。然后另存为word2003的xml文件。 2.格式化xml文件,占位符的位置用${testname}代替,若有多行格式相同数据等,用List循环。 注意:不要用Eclipse工具去格式化xml文件(会导致 ......

一、准备工作

1.创建一个03的word文档,动态的数据用占位符标志占位(如testname)。然后另存为word2003的xml文件。

2.格式化xml文件,占位符的位置用${testname}代替,若有多行格式相同数据等,用list循环。

  注意:不要用eclipse工具去格式化xml文件(会导致导出的word文件不能用office软件打开,但是pdf能打开,估计是pdf的容错率高于office),推荐使用firstobject工具格式化xml文件。

3.将xml文件(也可以改成ftl格式)存放到项目中指定位置。

3.下载freemarker的jar包。  

    freemarker根据模板生成word文件实现导出功能

二、前端

前端页面添加一个导出按钮,然后按钮添加点击事件,事件中跳转到所请求的controller层即可:

window.location.href='xxxcontroller/xxxmethod';

如有参数,直接添加到后边即可。

三、后台

1.编写工具类

  

package io.renren.common.utils;

import java.io.bufferedwriter;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstreamwriter;
import java.io.writer;
import java.net.urlencoder;
import java.text.simpledateformat;
import java.util.date;
import java.util.map;
import java.util.random;

import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import freemarker.template.configuration;
import freemarker.template.template;

/**
* 文件导出工具类
*
* @author zblwj
* @email 351094262@qq.com
* @date 2018年11月1日下午2:40:42
*/
public class wordutils {
/**
* 生成word文档
*/
@suppresswarnings("unchecked")
public static file createword(map datamap,string templatename,string filepath,string filename){

try {
//创建配置实例
configuration configuration = new configuration();

//设置编码
configuration.setdefaultencoding("utf-8");

//ftl模板文件
configuration.setclassfortemplateloading(wordutils.class,"/template");

//获取模板
template template = configuration.gettemplate(templatename);

//输出文件
file outfile = new file(filepath+file.separator+filename);
//如果输出目标文件夹不存在,则创建
if (!outfile.getparentfile().exists()){
outfile.getparentfile().mkdirs();
}
//将模板和数据模型合并生成文件
writer out = new bufferedwriter(new outputstreamwriter(new fileoutputstream(outfile),"utf-8"));
//生成文件
template.process(datamap, out);
//关闭流
out.flush();
out.close();
return outfile;
} catch (exception e) {
e.printstacktrace();
return null;
}
}

/**
* 生成文件名字
* @return
*/
public static string creatfilename() {
/** 文件名称,唯一字符串 */
random r = new random();
simpledateformat sdf1 = new simpledateformat("yyyymmdd");
stringbuffer sb = new stringbuffer();
sb.append(sdf1.format(new date()));
sb.append("_");
sb.append(r.nextint(100));
//文件唯一名称
string fileonlyname = "机关党支部党员积分申报表" + sb + ".doc";
return fileonlyname;
}

/**
* 导出文件
* @throws ioexception
*/
public static void exportmillcertificateword( httpservletresponse response, map map,string filepath,string templatename) throws ioexception {
file file = null;
inputstream fin = null;
servletoutputstream out = null;
try {
string filename = wordutils.creatfilename();
file = wordutils.createword(map, templatename, filepath,filename);
fin = new fileinputstream(file);
response.setcharacterencoding("utf-8");
response.setcontenttype("application/msword");
response.setheader("content-disposition", "attachment;filename=".concat(string.valueof(urlencoder.encode(filename, "utf-8"))));
out = response.getoutputstream();
byte[] buffer = new byte[512]; // 缓冲区
int bytestoread = -1;
// 通过循环将读入的word文件的内容输出到浏览器中
while((bytestoread = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytestoread);
}
}finally {
if(fin != null) fin.close();
if(out != null) out.close();
if(file != null) file.delete(); // 删除临时文件
}

}
}

2.controller层

3.server层

 

 三、最终结果

freemarker根据模板生成word文件实现导出功能

 四、个人总结

     此方法还是很简单,但是由于第一次使用,废了不少功夫。导出过程中会生成一个临时的文件,然后利用response的输出流将文件读取到浏览器客户端,读取完成后将会删除生成的临时文件。个人踩坑的地方是用eclipse格式化了xml文件,导致了导出的word文件不能用office工具打开。