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. 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。

    可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是 ...

  2. thymeleaf的onclick标签传参异常

    转自https://my.oschina.net/u/2312080/blog/2878183 异常 严重: Servlet.service() for servlet [DispatcherServ ...

  3. Hadoop第一式:配置Linux环境

    所有操作在虚拟机下完成,虚拟机软件选用VMware Workstation Pro 12 (后文简称为VM) 关于Linux安装不再阐述一.网络环境配置 1)Windows界面 首先在VM页面,点击虚 ...

  4. matplotlib figure图像-【老鱼学matplotlib】

    如果我们想要显示多个图像,有点类似多窗口显示图像这个概念,则就会用到plt.figure() 直接上例子: import numpy as np import pandas as pd import ...

  5. spark MLlib Classification and regression 学习

    二分类:SVMs,logistic regression,decision trees,random forests,gradient-boosted trees,naive Bayes 多分类:  ...

  6. easyui提交form表单接受数据处理、

    $('#Form').form('submit', { url:"withdrawal/bankAuthenticate4List.do", onSubmit: function( ...

  7. Gym 100963B

    Gym 100963B啊,郁闷,就tm调小了一点范围就A了,就写dp和贪心比较一下,范围到最大值的二倍-1就好了假设最大值的2倍以内能满足最优条件,当金额范围超过最大值2倍的时候:至于为什么,还不清楚 ...

  8. Nginx模块 ngx_http_limit_req_module 限制请求速率

    The ngx_http_limit_req_module module (0.7.21) is used to limit the request processing rate per a def ...

  9. Navicat Premium 最新版本12.1.16-64bit 完美破解,亲测可用!

    声明:本文只是提供一个网络上找到的针对12.1.16版本的破解注册机使用方式做一个说明,不建议企业用户破解,毕竟码农不容易,有条件的还是希望大家购买原版.当然个人学习用的但又不想购买原版的,这里只是提 ...

  10. jmeter基本组成原件介绍

    jmeter基本组成原件介绍 参考地址:https://wenku.baidu.com/view/d4986ca2aaea998fcc220ec1.html 从性能工具的原理划分: Jmeter工具和 ...