数据导出生成word附件使用POI的XWPFTemplate对象
比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel。
Jakarta POI 是一套用于访问微软格式文档的Java API。Jakarta POI有很多组件组成,其中有用于
操作Excel格式文件的HSSF和
用于操作Word的HWPF;

一、前端使用get请求和post请求都可以
get请求:
window.location.href= appPath+"/export/handoverForm?ticketId="+self.ticketId +
"&complainId="+row.id+"&formCode="+self.exportFormCode.complainDetail;
隐藏form表单改写成post请求,form表单get和post都可以:
<form id="exportForm" action="/service/xfComplain/exportCaseLedger" method="post">
<input type="hidden" name="dataRange" id="dataRange"/>
<input type="hidden" name="processStatus" id="processStatus"/>
<input type="hidden" name="cantonCode" id="cantonCode"/>
<input type="hidden" name="startDate" id="startDate"/>
<input type="hidden" name="endDate" id="endDate"/>
<input type="hidden" name="multiFildMatch" id="multiFildMatch"/>
</form>
$("#dataRange").val(self.table.pageData.dataRange);
$("#processStatus").val(self.table.pageData.processStatus);
$("#startDate").val(self.table.pageData.startDate);
$("#endDate").val(self.table.pageData.endDate);
$("#multiFildMatch").val(self.table.pageData.multiFildMatch);
$('#exportForm').submit();
二、java代码
@ResponseBody
@RequestMapping("/exportWord")
public void exportHandoverForm(HttpServletRequest request,
HttpServletResponse response, ExportParam exportParam) {
String projectPath = getProjectPath(request);
String templateFile = getTemplateFile(exportParam, projectPath, request);
Map<String, Object> data = new HashMap<>();
File file = null;
InputStream in = null;
ServletOutputStream out = null;
try {
// 数据源
HandoverModel handoverModel = getHandoverFormData(exportParam.getComplainId());
// 反射机制,获取所有属性对象,在拿到属性值,设置数据
for (Field field : HandoverModel.class.getDeclaredFields()) {
// 暴力反射,不是public修饰的属性也要获取
field.setAccessible(true);
data.put(field.getName(), field.get(handoverModel));
}
// 数据源组成map键值对形式
data.put("reportorList", handoverModel.getReportorList().getDatas());
String fileName = handoverModel.getCaseCode();
String exportFilePath = projectPath + ExportConstant.DEFAULT_EXPORT_PATH;
logger.info("----------templateFile:" + templateFile);
logger.info("-----------projectPath:" + projectPath);
// Configure对象是处理表格数据,可以自适应生成对应行数数据
Configure config = Configure.newBuilder().customPolicy("reportorList", new FileTablePolicy()).build();
// XWPFTemplate对象是处理word文档对象,根据map数据源的键值对渲染文档数据
XWPFTemplate template = XWPFTemplate.compile(templateFile, config).render(data);
// 文件生成到本地,先生成好文档,再读取到内存中响应给前台
String downloadFileName = fileName + ".docx";
File outPutFile = new File(exportFilePath);
if (!outPutFile.exists()) {
outPutFile.mkdirs();
}
FileOutputStream outFile = new FileOutputStream(exportFilePath + downloadFileName);
template.write(outFile);
outFile.flush();
outFile.close();
template.close();
// 通过文件流读取到文件,再将文件通过response的输出流,返回给页面下载
file = new File(projectPath + ExportConstant.DEFAULT_EXPORT_PATH + downloadFileName);
in = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(downloadFileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[512];
int bytesToRead = -1;
// 用响应对象response中的输出流读取生成好的文件
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (Exception e) {
logger.error("导出word出错", e);
} finally {
if (in != null) try {
in.close();
if (out != null) out.close();
if (file != null) file.delete(); // 删除临时文件
} catch (IOException e) {
logger.error("删除删除临时文件出错", e);
}
}
}
使用XWPFTemplate引擎,插件化Configure插入表格;

三、不需要插入表格:

XWPFTemplate template = XWPFTemplate.compile(templateFile).render(data);
数据导出生成word附件使用POI的XWPFTemplate对象的更多相关文章
- 数据导出生成Excel附件使用POI的HSSFWorkbook对象
比较常用的实现Java导入.导出Excel的技术有两种Jakarta POI和Java Excel.Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组 ...
- PowerDesigner将PDM导出生成WORD文档
PowerDesigner将PDM导出生成WORD文档 环境 PowerDesigner15 1.点击Report Temlates 制作模板 2.如果没有模板,单击New图标创建.有直接双击进入. ...
- 在ASP.NET中将GridView数据导出到Word、Excel
在ASP.NET中将GridView数据导出到Word.Excel asp.net,导出gridview数据到Word,Excel,PDF #region Export to Word, Exce ...
- java导出生成word(类似简历导出)
参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...
- java导出生成word
最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的 ...
- 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件
今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...
- gridview数据导出到word和excel以及excel的导入
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 使用NPOI将数据导出为word格式里的table
开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. ...
- 将HTML导出生成word文档
前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...
随机推荐
- RAID磁盘阵列和RAID配置
目录 一.RAID磁盘阵列介绍 二.RAID磁盘阵列分类 2.1.RAID 0(条带化存储) 2.2.RAID 1(镜像存储) 2.3.RAID 5 2.4.RAID 6 2.5.RAID 1+0和0 ...
- 洛谷P1377题解
题面 首先他叫我们建一颗笛卡尔树,所以我们就建一颗笛卡尔树. 然后他说要生成序列中最小的.想到笛卡尔树的一条性质:左<根<右.但是根节点必须先插进去.他的两个儿子用贪心的思想就知道是先选左 ...
- OpenStack镜像制作笔记 --以windows8.1-amd64为例
by hyc 目录 1.下载win8_64位的iso文件 2.下载对应电脑的vnc 3.下载Xshell软件 4.连接成功后,在Xshell下安装软件包 5.下载FileZilla Client软件 ...
- MobSF移动安全扫描平台环境搭建与试用
MobSF简介 MobSF(Mobile-Security-Framework)是一种开源自动化的移动应用程序(Android / iOS / Windows)安全测试框架,能够执行静态,动态和恶意软 ...
- 项目中redisTemplate设置的key,redis客户端上查询不到的问题
再项目使用了redis储存key,测试需要在客户端删除对应的key,发现查询不到对应的key redis客户端: 发现redisTemplate实际存进去的key会多了几个字符 原因:程序中对key没 ...
- Nginx整数溢出漏洞 Writeup
Nginx整数溢出漏洞 CVE-2017-7529 题目URL:http://www.whalwl.site:8040/ 漏洞描述 在 Nginx 的 range filter 中存在整数溢出漏洞,可 ...
- LDAP未授权访问学习
LDAP未授权访问学习 一.LDAP 介绍 LDAP的全称为Lightweight Directory Access Protocol(轻量级目录访问协议), 基于X.500标准, 支持 TCP/IP ...
- miniFTP项目实战一
项目简介: 在Linux环境下用C语言开发的Vsftpd的简化版本,拥有部分Vsftpd功能和相同的FTP协议,系统的主要架构采用多进程模型,每当有一个新的客户连接到达,主进程就会派生出一个ftp服务 ...
- 解决uni-file-picker组件在ios点击失效的问题
从组件源码里找到choose-and-upload-file.js文件,查找chooseAndUploadFile方法,在方法里添加下面代码,保存即可解决. opts.sourceType = ['a ...
- Spring-JDBC表情符号不能存入数据库
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4; JDBC解决方案: //params List<Object> param ...