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. 重磅发布 | Serverless 应用中心:Serverless 应用全生命周期管理平台

    ​简介:Serverless 应用中心,是阿里云 Serverless 应用全生命周期管理平台.通过 Serverless 应用中心,用户在部署应用之前无需进行额外的克隆.构建.打包和发布操作,即可快 ...

  2. 阿里云CDN产品经理陈章炜:边缘创新技术和落地实践

    简介: CDN除了加速外,不断被赋予更多价值.在阿里云CDN推出的<极速奔跑吧 2021>首场直播中,阿里云架构师和产品经理不仅对近期阿里云发布的CDN产品最佳实践图进行了详细解读,还对C ...

  3. Flink 和 Pulsar 的批流融合

    简介: 如何通过 Apache Pulsar 原生的存储计算分离的架构提供批流融合的基础,以及 Apache Pulsar 如何与 Flink 结合,实现批流一体的计算. 简介:StreamNativ ...

  4. WPF 在 .NET Core 3.1.19 版本 触摸笔迹偏移问题

    在更新到 .NET 6 发布之前的,在 2021.11.02 的 .NET Core 版本,都会存在此问题.在 WPF 应用里面,如果在高 DPI 下,进行触摸书写,此时的笔迹将会偏移.核心原因是在这 ...

  5. WPF 通过 InputManager 模拟调度触摸事件

    在 WPF 中,框架可以分为两个部分,一个是渲染,另一个是交互.交互的入口是在 InputManager 里面,而实际的交互实现需要通过渲染布局和交互的路由事件才能完成.在输入管理提供了调度事件的方法 ...

  6. 2018-12-27-WPF-从-DrawingVisual-转-BitmapImage-图片

    title author date CreateTime categories WPF 从 DrawingVisual 转 BitmapImage 图片 lindexi 2018-12-27 11:3 ...

  7. 16.prometheus监控总结

    一.监控流程总结 1.需要在被监控的服务器上安装xx_exporter来收集数据(可以是源码安装,最好用docker.docker-compose) 2.添加Prometheus配置,去收集(xx_e ...

  8. RMBG1.4服务器部署指南

    近期,一家AIGC公司BRIA开源了一个出圈的模型:RMBG-1.4,它可以实现高质量地一键去除图片中的背景.下面是一些具体的例子,可以看到这个模型可以实现非常精细的"抠图". R ...

  9. uniapp中OnShow获取缓存

    点击查看代码 function getStorage(key) { return new Promise((resolve, reject) => { uni.getStorage({ key: ...

  10. C语言:输入一个整数并让其逆反输出。123->321

    主要思想为: a)计算输入的位数有多少个 b)计算出最高位的单位(若最高位为是万位, 那么需要一个变量存储最高位数值1000) c)用取模的方法从个位数开始进行取出每一个单位上的数字 d)从个位数开始 ...