1、前言

jeecg 中已经自带 word 的导出导出功能,其所使用的也是 easypoi,尽管所导出的 word 能满足大部分需求,

但总是有需要用到自定义 word导出模板,下文所用到的皆是 easypoi 提供的,为方便下次翻阅,故记之。

2、代码部分

2.1、controller

@RequestMapping("/ftl2word")
public void velocity2word(JeecgDemoExcelEntity jeecgDemoExcel, HttpServletRequest request,
HttpServletResponse response) throws IOException {
try {
jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId());
List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart");
String docFileName = "word-模板导出测试.doc";
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("info", jeecgDemoExcel);
rootMap.put("departs", departs);
// FreemarkerUtil.createFile("exportMyExcel.xls",
// docFileName,rootMap, request, response,
// FreemarkerUtil.EXCEL_FILE);
FreemarkerUtil.createFile("ftl2doc.ftl", docFileName, rootMap, request, response, FreemarkerUtil.WORD_FILE);
} catch (Exception e) {
e.printStackTrace();
}
}

2.2、entity

实体就不扔出来了,详细说一下这个地方:

jeecgDemoExcel = this.jeecgDemoExcelService.getEntity(JeecgDemoExcelEntity.class, jeecgDemoExcel.getId());
List<Map<String, Object>> departs = this.systemService.findForJdbc("select id,departname from t_s_depart");
String docFileName = "word-模板导出测试.doc";
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("info", jeecgDemoExcel);
rootMap.put("departs", departs);

jeecgDemoExcel  为 List<实体>,departs 为 List<Map<String, Object>>,怎么用?ftl 语法了解一下?

2.3、工具类 FreemarkerUtil

public class FreemarkerUtil {
private static final Object LOCK = new Object();
/**
* word文件
*/
public static final int WORD_FILE = ;
/**
* excel文件
*/
public static final int EXCEL_FILE = ; private static Configuration cfg; private static FreemarkerUtil ftl ; private FreemarkerUtil(String templateFolder) throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(templateFolder));
cfg.setObjectWrapper(new DefaultObjectWrapper());
} private static void check(HttpServletRequest request) {
if (ftl == null) {
synchronized (LOCK) {
try {
ftl = new FreemarkerUtil(request.getServletContext().getRealPath("/")+"export/template");
} catch (IOException e) {
e.printStackTrace();
}
}
} } /**
* 创建 word 文档
* 必须先设置response导出配置,然后解析模版,否则会出问题
* @throws IOException
*/
public static void createFile(String templateName,String docFileName, Map<String,Object> rootMap,HttpServletRequest request, HttpServletResponse response,int fileType) throws IOException {
// response.resetBuffer();
//设置导出
response.addHeader("Cache-Control","no-cache");
response.setCharacterEncoding("UTF-8");
if( WORD_FILE == fileType){
response.setContentType("application/vnd.ms-word;charset=UTF-8");
}else if(EXCEL_FILE == fileType){
response.setContentType("application/octet-stream;charset=UTF-8");
}else{
response.setContentType("application/octet-stream");
}
String ua = request.getHeader("user-agent");
ua = ua == null ? null : ua.toLowerCase();
if(ua != null && (ua.indexOf("firefox") > || ua.indexOf("safari")>)){
try {
docFileName = new String(docFileName.getBytes(),"ISO8859-1");
response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
} catch (Exception e) {
}
}else{
try {
docFileName = URLEncoder.encode(docFileName, "utf-8");
response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
} catch (Exception e) {
}
}
check(request);
//解析模版
Template temp = cfg.getTemplate(templateName, "UTF-8");
PrintWriter write = response.getWriter();
try {
temp.process(rootMap, write);
} catch (TemplateException e) {
e.printStackTrace();
}finally {
if(write != null){
write.flush();
write.close();
}
}
}
}

2.4、ftl 模板

https://files.cnblogs.com/files/niceyoo/ftl2doc.rar

至于,ftl 如何生成,以及如何写,可自定查询,后面也会单独文章补充。

博客地址:http://www.cnblogs.com/niceyoo

 18年专科毕业后,期间一度迷茫,最近我创建了一个公众号用来记录自己的成长。 

8、jeecg 笔记之 自定义word 模板导出(一)的更多相关文章

  1. 6、jeecg 笔记之 自定义excel 模板导出(一)

    1.前言 jeecg 中已经自带 excel 的导出导出功能,其所使用的是 easypoi,尽管所导出的 excel 能满足大部分需求, 但总是有需要用到自定义 excel 导出模板,下文所用到的皆是 ...

  2. Net Core DocXCore 实现word模板导出

    实际工作中,往往有这样的需求,需要导出word,还有各种各样的样式,于是有了word模板导出. 实现以下几个需求: 1.表单导出 2.表格导出 3.表单表格混合导出 4.实际用例测试 解决方案: 实现 ...

  3. 自定义 Word 模板

    自定义 Word 模板 目录 必要设置 样式设置 标题样式 多级列表 封面 正文 引用目录 页码 页眉 图标 自定义模板保存 样式导入和导出 批量删除多余空白段落 必要设置 显示所有格式标记 选择&q ...

  4. SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?

    前文我们介绍了通过Apache POI通过来导出word的例子:那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个 ...

  5. word模板导出的几种方式:第一种:占位符替换模板导出(只适用于word中含有表格形式的)

    1.占位符替换模板导出(只适用于word中含有表格形式的): /// <summary> /// 使用替换模板进行到处word文件 /// </summary> public ...

  6. .net core 使用NPOI填充Word模板导出Word

    最近工作用到在Word模板插入数据库数据,导出一个带数据的Word文件,想起来之前操作Word都是用微软提供的Microsoft.Office.Interop.Word,而在最新的..NET CORE ...

  7. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  8. C# 使用Word模板导出数据

    使用NPOI控件导出数据到Word模板中方式: 效果如下: Word模板: 运行结果: 实现如下: Student.cs using System; using System.Collections. ...

  9. OpenXml Sdk 根据Word模板导出到word

    一:OpenXml Sdk 简介 Open XML标准的简单介绍:Ecma Office Open XML(“Open XML”)是针对字处理文档.演示文稿和电子表格的国际化开放标准,可免费供多个应用 ...

随机推荐

  1. unity 网页加载AB问题

    下载一次后会缓存,清理一下就能加载新的同名AB了 AssetBundle.onload

  2. sqlserver(查看被锁进程)

    -- ###### 查看被锁进程 ###### select 标志, 进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid, 数据库名=db_name(dbid), ...

  3. Emacs Org-mode 1 下载、安装、基本使用

    1.1 总述 Org 是一种帮助我们做笔记.日常事件或者项目计划的快速高效的文本格式系统. Org 有以下特点: Org mode 基于组织结构(outline-mode)对文本进行组织.具有良好的快 ...

  4. C# 实现登录并跳转界面

    Program.cs文件添加如下内容 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(f ...

  5. Logstash利用ruby将有用的日志放到一个ES_INDEX将无用的日志放到另一个ES_INDEX

    input{ kafka { bootstrap_servers => "127.0.0.1:9092" client_id => "nginxlog&quo ...

  6. Xshell 连接 vmware中的CentOS 7

    参考内容:             Xshell 连接 CentOS 7 与 Ubuntu Server,http://www.linuxidc.com/Linux/2017-03/141333.ht ...

  7. Linux-vi编辑器简单使用(保证存活)

    vi编辑器基本模式 命令行模式(command mode) 光标移动.复制粘贴.删除 插入模式(insert mode) 文字输入 底行模式(last line mode) 保存.退出 模式转换 co ...

  8. 识别拖动与点击操作之zepto的bug

    问题描述:给页面<a>标签绑定了tap事件,在移动设备上点击按钮貌似一切正常,可以响应.但是,把页面上下滑动几次之后,或者在滑动时手指滑动出移动屏幕之外,之后再点击按钮,就会发现第一次点击 ...

  9. 设计模式之架构型MVC,MVP,MVVM模式

    一.MVCMVC,Model View Controller,是软件架构中最常见的一种设计模式,简单来说就是通过Controller的控制去操作Model层的数据,并且返回给view层展示.View跟 ...

  10. 微信小程序做radio,可以拖动进度条

    很简单的一个音乐播放器 data:{ src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4a ...