1.功能概述:
web项目中,在html文件点击下载word文件,后台获取要输出的数据再导出到word文档中
2. 操作步骤:
(1).新建word模板,凡是需要填充的数据用${xxxx},编辑好word文档后,另存为word文档的(*.xml),命名为word.xml(名字不可更改)(2).在web项目中webroot目录下新建template文件夹,并将word.xml文件放到template文件夹下
(3).拷贝DocUtil.java工具类到项目中,如下面的调用实例
(4).界面上一个点击下载的按钮,访问该controller方法即可
3.依赖的maven文件:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency>
4.java工具代码:
/*** @文件名称: DocUtil.java * @描述: TODO * @作者: wuwh* @时间:4月10日 上午10:36:08 * @版本:V1.0*/ import java.io.BufferedInputStream;import java.io.BufferedOutputStream;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.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import .URLEncoder;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import freemarker.template.Configuration;import freemarker.template.Template;/** * @类功能说明: 1.该代码在web项目中调用使用,需要在项目中的webroot目录下新建一个template文件夹,然后将预定义word的模板转成word.xml文件放入template文件下即可* 2.将要输出的数据放到map集合中,作为参数传入即可依赖jar包:freemarker-2.3.13.jar* @作者: wuwh* @创建时间:4月10日 上午10:36:08 * @版本:V1.0 */public class DocUtil {public static void download(HttpServletRequest request,HttpServletResponse response,String newWordName,Map dataMap) {Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8"); //注意这里要设置编码//模板文件word.xml是放在WebRoot/template目录下的configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template");Template t = null;try {//word.xml是要生成Word文件的模板文件t = configuration.getTemplate("word.xml","utf-8"); // 文件名 还有这里要设置编码} catch (Exception e) {e.printStackTrace();}File outFile = null;Writer out = null;String filename = newWordName;try {outFile = new File(newWordName);out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8")); //还有这里要设置编码} catch (Exception e1) {e1.printStackTrace();}try {t.process(dataMap, out);} catch (Exception e) {e.printStackTrace();}try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}InputStream fis = null;OutputStream toClient = null;try {fis = new BufferedInputStream(new FileInputStream(outFile));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的Headerfilename = URLEncoder.encode(filename, "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称response.addHeader("Content-Disposition", "attachment;filename=" + filename+"");response.addHeader("Content-Length", "" + outFile.length());toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();} catch (Exception e) {e.printStackTrace();} finally{try {if(fis!=null){fis.close();}} catch (IOException e) {e.printStackTrace();}try {if(toClient!=null){toClient.close();}} catch (Exception e) {e.printStackTrace();}}}}
5.调用示例:
@RequestMapping("getDoc")public void getDoc( HttpServletRequest request,HttpServletResponse response) {MessageVO vo = new MessageVO();Map<String,String> dataMap = new HashMap<String,String>();dataMap.put("title", "个人信息");dataMap.put("name", "wuhui");dataMap.put("age", "18");dataMap.put("birthday", "2000-11-11");dataMap.put("address", "福建省福州市晋安区");String newWordName = "信息.doc";//调用打印word的函数DocUtil.download(request, response, newWordName, dataMap);}
6.结果图: