java后台利用模板生成Word文档提供前台下载
原文地址:https://blog.****.net/u010251278/article/details/52398578
2016.12.06更新,java后台利用Apache poi生成Excel文档提供前台下载,博客链接http://blog.****.net/u010251278/article/details/53491258
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在博主完成尚未完成的web项目的过程中,又遇到了这样一个需求,当用户点击相关按钮时,系统依据模板生成一份Word文档并提供下载。
实现的依据参考了这篇博客,现在将我的实现过程记录下来,为后人行个方便也为自己留个记录。
首先要指出的是,实现的思路和freeMarker差不离,将.doc的文档做相应的转换后转为.ftl文档,其中的变量会以${xxx}来代替,这样就可以
使用freeMarker模板引擎来替换变量啦为例,动态生成文件后发送到浏览器端提供用户下载。所以,实现该功能分为以下几个步骤,以我的
项目为例,一些敏感的信息可能不予展示,但不影响该功能的学习。
1.为你的项目导入freeMarker包
我的项目是依靠maven来维护依赖的,所以引入很方便,只需要在pom文件中加入下面这个依赖就好
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
如果还是比较传统的话,就像那个链接里的项目一样,把jar包导入项目吧
2.依据模板动态生成word文档,首先你得有个模板
模板是doc类型就好,注意不是docx,docx没有尝试,doc类型已经满足了我的需求,朋友们如果尝试成功了可以告诉我一下
test.doc,注意“产品品质证明书”是一张图片哦,图片会漂亮的留在生成的新文档中。将变量替换成${xxx}即可,这里只选了两
个变量
3.点击 文件->另存为 将test.doc保存为xml类型,即 Word XML文档 .xml类型 ,得到test.xml
4.用notepad或者sublime打开test.xml,你会发现${xxx}会被分割成${*********xxx********)的样子,将*******删除,保证它又成了完整的变量
标签,像这样
5.将文件已utf-8编码保存,另存为为.ftl,找不到该格式直接改文件后缀名就行,这样得到test.ftl
6.前台触发事件
我的项目是基于SpringMVC的,所以前台触发只需要在view层的文件里加个按钮事件即可,直接上代码
- function generateMillCertificate(id) {//点击下载按钮触发的事件
- window.location.href = '../deliveryOrder/exportMillCertificate?id=' + id;
- }
这里又分为两步
a.controller层接收请求,根据参数拼凑数据,放在map中
- /***
- * 导出Word材质单
- *
- * @return
- * @throws Exception
- */
- @RequestMapping(value = "exportMillCertificate", method = RequestMethod.GET)
- @ResponseBody
- public void exportMillCertificate(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- //获得数据,系统相关,就不展示了
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("customerShortName",deliveryOrder.getRepositoryName());
- map.put("productName",deliveryOrderDetail.getProductName());
- WordUtils.exportMillCertificateWord(request,response,map);
- }
生成新的文档,通过response返回给浏览器并提供用户下载
- public class WordUtils {
- //配置信息,代码本身写的还是很可读的,就不过多注解了
- private static Configuration configuration = null;
- //这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
- private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "asserts/templete/";
- static {
- configuration = new Configuration();
- configuration.setDefaultEncoding("utf-8");
- try {
- configuration.setDirectoryForTemplateLoading(new File(templateFolder));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private WordUtils() {
- throw new AssertionError();
- }
- public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {
- Template freemarkerTemplate = configuration.getTemplate("test.ftl");
- File file = null;
- InputStream fin = null;
- ServletOutputStream out = null;
- try {
- // 调用工具类的createDoc方法生成Word文档
- file = createDoc(map,freemarkerTemplate);
- fin = new FileInputStream(file);
- response.setCharacterEncoding("utf-8");
- response.setContentType("application/msword");
- // 设置浏览器以下载的方式处理该文件名
- String fileName = "材质单"+DateUtils.curDateTimeStr14() + ".doc";
- 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(); // 删除临时文件
- }
- }
- private static File createDoc(Map<?, ?> dataMap, Template template) {
- String name = "test.doc";
- File f = new File(name);
- Template t = template;
- try {
- // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
- Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
- t.process(dataMap, w);
- w.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- return f;
- }
- }
是不是完美的一腿。写到这里就结束啦。