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. c4b和c4f的区别

    1. cc.c4b的参数直接填rgba的值. 2 .cc.c4f(r,g,b,透明度),把rgb值填进去,会发现颜色不对,需要把rgb值除以255,所以最终转换公式是: cc.c3b(r,g,b) = ...

  2. Python-form表单标签

    语义:标记表单 #1.什么是表单? 表单就是专门用来接收用户输入或采集用户信息的 #2.表单的格式 <form> <表单元素> </form> 链接:https:/ ...

  3. vue+cordova插件使用,bluetoothSerial.connect()连接失败

    这是GitHub地址https://github.com/don/BluetoothSerial

  4. redis-list操作

    List操作,redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name,values) # 在name对应的list中添加元素,每个新的元素都添加到列表的最 ...

  5. JAVA 动态代理原理和实现

    在 Java 中动态代理和代理都很常见,几乎是所有主流框架都用到过的知识.在面试中也是经常被提到的话题,于是便总结了本文. Java动态代理的基本原理为:被代理对象需要实现某个接口(这是前提),代理对 ...

  6. python实现简单的购物车

    import json,timeuserinfo={"lanfei": { "passwd":"lanfei", "salary& ...

  7. Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. 蒙特卡诺近似与PBM

    介绍蒙特卡诺近似的例子代码 #include<fstream> #include<iostream> #include<cstdlib> #include<c ...

  9. Burnside引理与Polya定理 学习笔记

    原文链接www.cnblogs.com/zhouzhendong/p/Burnside-Polya.html 问题模型 有一个长度为 $n$ 的序列,序列中的每一个元素有 $m$ 种取值. 如果两个序 ...

  10. MATLAB 2012b license checkout failed

    we offer you two ways to license matlab r2012b: standalone1) choose "install manually without u ...