比较常用的实现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对象的更多相关文章

  1. 数据导出生成Excel附件使用POI的HSSFWorkbook对象

    比较常用的实现Java导入.导出Excel的技术有两种Jakarta POI和Java Excel.Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组 ...

  2. PowerDesigner将PDM导出生成WORD文档

    PowerDesigner将PDM导出生成WORD文档 环境 PowerDesigner15 1.点击Report Temlates 制作模板 2.如果没有模板,单击New图标创建.有直接双击进入. ...

  3. 在ASP.NET中将GridView数据导出到Word、Excel

    在ASP.NET中将GridView数据导出到Word.Excel asp.net,导出gridview数据到Word,Excel,PDF   #region Export to Word, Exce ...

  4. java导出生成word(类似简历导出)

    参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...

  5. java导出生成word

    最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的 ...

  6. 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件

    今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...

  7. gridview数据导出到word和excel以及excel的导入

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  8. 使用NPOI将数据导出为word格式里的table

    开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. ...

  9. 将HTML导出生成word文档

    前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...

随机推荐

  1. 内置函数 strlen

    1 //内置函数 strlen 2 //计算字符串的实际长度,不含字符串结束标准\0 3 4 #include<stdio.h> 5 #include<stdlib.h> 6 ...

  2. 有赞Android实习五面都挂了,复习半月再战,转拿腾讯offer!

    缘起 为了有赞的面试准备了半个月的样子,当时还投了美团.字节.滴滴.京东,目的只有一个,就是要进大厂,但是只有有赞扛过了一面,其他都是一面就挂了. 前三面都自我感觉良好,以为能稳拿offer的,没想到 ...

  3. springboot的单元测试(总结两种)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  4. linux 磁盘IO速度测试

    写入速度测试命令:time dd oflag=direct if=/dev/zero of=/data2/test bs=2k count=1000000 //if表示从哪里读取 of表示写入到哪里 ...

  5. Blazor+Dapr+K8s微服务之服务调用

    1.1         Dapr环境配置 1.1.1        在开发机安装Docker Desktop并启用Kubernetes 安装过程略,安装好后效果如下:(左下角两个绿色指示Docker和 ...

  6. Mysql报错注入之floor报错详解

    一.简述 利用 select count(*),(floor(rand(0)*2))x from table group by x,导致数据库报错,通过 concat 函数,连接注入语句与 floor ...

  7. Spring-Boot注入自定义properties文件配置

    创建wzq.properties wzq.properties注入User实体类中 @PropertySource(value = "classpath:wzq.properties&quo ...

  8. 软件研发中也有5S 管理?

    在精益生产中,价值流是贯穿生产全程的关键要素,标准化作业是实现生产线同步的关键工具,而生产现场的5S 管理则是管理一切生产要素的基础,所以我们将5S 称为精益的基础并不为过.5S 管理不仅可以应用到车 ...

  9. Spark入门:Spark运行架构(Python版)

    此文为个人学习笔记如需系统学习请访问http://dblab.xmu.edu.cn/blog/1709-2/ 基本概念 *  RDD:是弹性分布式数据集(Resilient Distributed ...

  10. SpringBoot以jar包部署需要注意的thymeleaf页面映射问题

    关于themeleaf映射需要注意的: 1.页面映射 所有静态页面映射的时候,mapping后面要以/开头(最好),不以/开头也行 但是return 后面路径不能以/开头:IDE中正常,但是打jar包 ...