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. 源码解读:KubeVela 是如何将 appfile 转换为 K8s 特定资源对象的

    简介: KubeVela 是一个简单易用又高度可扩展的云原生应用管理引擎,是基于 Kubernetes 及阿里云与微软云共同发布的云原生应用开发模型 OAM 构建.本文主要目的是探索 KubeVela ...

  2. 业界首个机密计算容器运行时—Inclavare Containers正式进入CNCF!

    ​简介: Inclavare Containers 通过云原生计算基金会(CNCF)TOC 投票正式成为 CNCF 官方沙箱项目. 作者|彦荣 ​ 2021 年 9月 15 日,Inclavare C ...

  3. [Go] go build 和 go install 的区别

    $ go build 源文件及其包依赖 编译成二进制. install 不仅执行build过程 而且会把编译的二进制放到 $GOPATH/bin/,包放到 $GOPATH/pkg/ Link:http ...

  4. 7.prometheus监控--监控docker

    4.监控docker 为了能够获取到Docker容器的运行状态,用户可以通过Docker的stats命令获取到当前主机上运行容器的统计信息,可以查看容器的CPU利用率.内存使用量.网络IO总量以及磁盘 ...

  5. LVGL 显示图片

    一.图片存储 我们可以将图像存储在两个位置 作为内部存储器(RAM或ROM)中的变量 作为文件 图片以文件的形式存储在文件系中(比如SD),需要打开LVGL的文件操作的功能(打开,读取,关闭等).虽然 ...

  6. 自制一个发送验证码的10秒倒计时js效果

    <template>     <div class="conten1">         <input class="code"  ...

  7. MinDoc 编译安装(linux环境)

    目录 MinDoc 简介 项目地址: 下面以 Linux 系统为例: Gomod方式安装 下载项目代码到本地 写入依赖 下载依赖 创建数据库 配置数据库 编译main.go 提升文件权限 初使化数据库 ...

  8. Golang ETCD包的安装使用 golang安装etcd第三方库clientv3 报错 解决

    目录 ETCD 官网 视频地址 课件资料 ETCD 日志收集项目 为什么要自己写不用ELK? 项目的架构 上节课项目进度 etcd介绍 安装etcd windows安装 Ubuntu(linux系统) ...

  9. TeamViewer 免费版的安全性和隐私问题

    TeamViewer 靠个免费个人版获取了大量用户,但也因为这个遭遇大量吐槽.国内用户吐槽的焦点在于"被提示用于商业用途,要求购买许可,但是商业许可又太贵". 接下来这份新闻,我们 ...

  10. .NET 代理模式(一)基本概念

    代理模式 代理模式,它是一种结构型的设计模式. 让你能够提供对象的替代品或其占位符. 代理控制着对于原对象的访问, 并允许在将请求提交给对象前后进行一些处理. 简单理解就是 客户端不会直接与实际实现类 ...