1.生成模板的工具类

package com.jesims.busfundcallnew.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.activiti.engine.impl.bpmn.data.Data; import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; public class WordUtil {
public static void createWord(Map<String,Object> dataMap, String templateName, String filePath, String fileName){
try {
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8"); //设置编码
//configuration.setDefaultEncoding("UTF-8"); //// ftl模板文件统一放至 com.lun.template 包下面
// configuration.setClassForTemplateLoading(this.getClass(), "com.jesims.modules.freemarker.entity");
// request.getSession().getServletContext().getRealPath("/WEB-INF/ftl") 获取文件夹下面的所有模版
// configuration.setDirectoryForTemplateLoading(new File(request.getSession().getServletContext().getRealPath("/WEB-INF/ftl"))); configuration.setDirectoryForTemplateLoading(new File("C:\\Project\\huajinziben\\hjzb2\\src\\main\\webapp\\WEB-INF\\attachment"));// 本地模板路径
//获取模板
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(); } catch (Exception e) {
e.printStackTrace();
}
}

public static void download (HttpServletResponse response,String pathName,String fileName){
OutputStream outputStream = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream inStream = null;
try {
// 读到流中
File file = new File(pathName);
inStream = new FileInputStream(file);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".doc").getBytes(), "iso-8859-1"));
outputStream = response.getOutputStream();
bis = new BufferedInputStream(inStream);
bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bis) {
bis.close();
}
if (null != bos) {
bos.close();
}
if (null != outputStream) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
}
}

}
}

2.调用

3.定义好模板

4.转成ftl文件

首先转成 Word 2003 XML 文档

然后将 xml后缀改为 ftl

5.替换关键字

6.前端调用并下载word

前端 绑定点击事件:

 注 :前端下载 需要用  

window.location.href  
不然不能下载

下载word

OutputStream outputStream = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream inStream = null;
try {
// 读到流中
File file = new File("C:\\Users\\18300\\Desktop\\aaa\\基金call款通知书.doc"); //文件路径+文件名称
inStream = new FileInputStream(file);
String fileName = "基金call款通知书";// 生成的文件名称
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".doc").getBytes(), "iso-8859-1")); outputStream = response.getOutputStream(); bis = new BufferedInputStream(inStream);
bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bis) {
bis.close();
}
if (null != bos) {
bos.close();
}
if (null != outputStream) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
}
}

7.导出结果

 

ftl生成模板并从前台下载的更多相关文章

  1. JXL生成Excel,并提供下载(2:提供下载)

    实现效果: 项目中使用JXL生成Excel,使生成的Excel可以让用户下载,如下图 一.生成Excel 二.提供下载 实现思路: 1.页面使用form表单提交方式(Ajax提交方式,我这里不行) 2 ...

  2. mybatis配eclise模板,mybatis快速生成模板

    eclipse中mybatis得mapper文件不提示(mybatis-3-mapper.dtd,mybatis-3-config.dtd) 1.下载该文件到你的硬盘文件夹下 2.windows -- ...

  3. 20款时尚的 WordPress 企业模板【免费主题下载】

    在这篇文章中,我们收集了20款时尚的 WordPress 企业模板.WordPress 作为最流行的博客系统,插件众多,易于扩充功能.安装和使用都非常方便,而且有许多第三方开发的免费模板,安装方式简单 ...

  4. [BILL WEI]stimulsoft reports DEMO自动生成模板

    stimulsoft reports是一款强大的报表开发工具,能够开发各式各样的报表. 对于初学者而言,任何报表开发,刚开始都是去模仿,熟练掌握之后,自己才能独立开发,而在报表开发实际过程中, 我们所 ...

  5. ionic生成apk使用build命令下载gradle-2.2.1-all.zip卡,解决方案

    ionic生成apk使用build命令下载gradle-2.2.1-all.zip卡,解决方案 直接使用ionic build android命令,自动下载gradle-2.2.1-all.zip超慢 ...

  6. C# T4 模板 数据库实体类生成模板(带注释,娱乐用)

     说明:..,有些工具生成实体类没注释,不能和SqlServer的MS_Description属性一起使用,然后照着网上的资源,随便写了个生成模板,自娱自乐向,其实卵用都没有参考教程    1.htt ...

  7. springboot mail整合freemark实现动态生成模板

    目标:1:springboot 整合 mail2: mail 使用freemark 实现模板动态生成(就是通过字符串生成模板,不需要在工程中写入固定模板)3: springboot 整合aop 实现日 ...

  8. uniapp - 更改项目生成模板、页面

    每次生成项目目录都需要删除一些再添加太麻烦了,就想着能不能修改一下模板 - 当然自定义模板也能实现 好了,被我找到了. 修改以后源文件名称和格式覆盖回去即可,重新启动hbuilderx 这里可以修改e ...

  9. Android Studio 配置快速生成模板代码

    前言 Android studio 有提供快速生成模板代码的功能,其实这个功能也可以自定义配置.此篇博客将讲解如何使用此功能 进入Settings 选择 Editor > Live Templa ...

  10. 【CF1443E】Long Permutation 题解(排列生成模板)

    原题链接 题意简介 给定一个长度为 n 的排列 {1,2,3,...,n} .现有两种操作: 对某个区间 [l,r] 求和 将排列往后推 x 次 (按字典序) 其中 \(n,q \leq 2\time ...

随机推荐

  1. 每次都需要解释大量指令?使用 PolarDB-X 向量化引擎

    简介: 向量化引擎为PolarDB-X的表达式计算带来了显著的性能提升. 介绍 PolarDB-X是阿里巴巴自研的云原生分布式数据库,采用了计算-存储分离的架构,其中计算节点承担着大量的表达式计算任务 ...

  2. 运营也用的起来的数据分析工具:Quick BI即席分析详解

    ​简介: 数据部门是一个容易被投诉的"高危"部门,需求响应慢.数据准确性不高会影响业务的发展. 然而数据分析师每周动辄就有几十个需求在手,无限的加班也无法解决所有问题,到底怎样才能 ...

  3. SQL SERVER数据库存储过程加密

    CREATE PROCEDURE [dbo].[kytj_Base_Worker] WITH ENCRYPTION AS SELECT u.worker_number, u.worker_name, ...

  4. Jenkins+Harbor+gogs+docker+portainer+springboot实现devOps(企业实战)

    本篇主要讲述springboot以及vue前后端分离项目,使用Jenkins拉取gogs代码仓库源码,构建Docker镜像并推送至Harbor仓库,使用docker 可视化部署工具[portainer ...

  5. 移动端、微信小程序兼容性问题汇总(持续更新……

    1. safari浏览器字体不能自动随网页缩放调整大小 -webkit-text-size-adjust:100% 2. 点击<button><input>有灰色透明背景 -w ...

  6. Codeforces Round 917 (Div. 2)

    A. Least Product 存在 \(a[i] = 0\),\(min = 0\),不需要任何操作. 负数个数为偶数(包括0),\(min = 0\),把任意一个改为 \(0\). 负数个数为奇 ...

  7. Centos7 忘记密码的解决方法

    ilo远程修改 重启服务器,点击cold boot 按钮. 开机后,进入内核上按"e",进入编辑模式 在linux删除linux16这一行的地方,写入如下语句,在*.img行之前. ...

  8. 安装conda搭建python环境(保姆级教程)

    参考文档: 安装conda搭建python环境(保姆级教程)

  9. GOLANG-配置nginx反向代理端口 配置域名

    目录 配置/etc/nginx/nginx.conf文件 新建/etc/nginx/conf.d/doc.haimait.conf文件 重启nginx服务 解析自己的域名到服务器的公网ip 配置/et ...

  10. JavaScript实现全选选项框的功能和鼠标划入出现下拉框的功能--JavaScript实例集锦(初学)

    有一些论坛,文章后台编辑都会出现选择框的操作. 1.实现选项框全选和取消全选的功能: 代码实现: <!DOCTYPE html> <html> <head> < ...