SpringBoot向Excel模板中写入数据并下载 (无需获取file对象及模板绝对路径)
之前用获取模板路径的方式测试没问题打包后就有问题了

莫名出现一个! 找了很多教程尝试无果 最终使用下面这个方式
无需获取file对象以及模板路径的方式进行写入下载
(那个设置浏览器编码没有测试不知道能不能用!!!)
public void export(SampleFilterAO filter, HttpServletResponse response, HttpServletRequest request) {
Map<String, Object> map = new HashMap<>();
// 获取导出的时间参数
String date = request.getParameter("Date");
map.put("Date", date);
String fileName = "data.xlsx";
// 使用类加载器获取excel文件流,基于模板填充数据
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream is = null;
XSSFWorkbook workbook = null;
try {
is = classPathResource.getInputStream();
workbook = new XSSFWorkbook(is);
XSSFSheet sheet = null;
// 获取第一个sheet页
// getSheet的参数是sheet的名称, 获取具体名称的sheet。
sheet = workbook.getSheetAt(0);
Long offset = (filter.getPage() - 1) * filter.getLimit();
filter.setOffset(offset);
filter.setLimit(sampleMapper.count(filter).intValue());
List<Sample> resutList = sampleMapper.list(filter);
for (int i = 0; i < resutList.size(); i++) {
Integer j=i+1;
writeExcel(sheet, resutList, j, i);
}
} catch (IOException e) {
e.printStackTrace();
}
//文件下載
response.reset();
response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/x-msdownload");
String newName = "";
try {
newName = URLEncoder.encode("扫描记录导出" + System.currentTimeMillis() + ".xlsx", "UTF-8");
String s = encodeFileName(request,newName);
response.addHeader("Content-Disposition", "attachment;filename=\"" + s + "\"");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
workbook.write(toClient);
toClient.flush();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//写入数据的方法
public void writeExcel(XSSFSheet sheet, List<Sample> resutList,Integer rownum, Integer index) {
if (resutList.get(index) != null && !"".equals(resutList.get(index))) {
Row row = sheet.createRow(rownum);
Cell cell = row.createCell(0); //序号
cell.setCellValue(index+1);
String sampleNo = resutList.get(index).getSampleNo();// 玻片编号
cell = row.createCell(1);
cell.setCellValue(sampleNo);
String patientNo = resutList.get(index).getPatientNo();// 病案号
cell = row.createCell(2);
cell.setCellValue(patientNo);
String patientName = resutList.get(index).getPatientName();// 姓名
cell = row.createCell(3);
cell.setCellValue(patientName);
String patientSex = resutList.get(index).getPatientSex();// 性别
cell = row.createCell(4);
cell.setCellValue(patientSex);
String position = resutList.get(index).getPosition();// 部位
cell = row.createCell(5);
cell.setCellValue(position);
String aiResultId = resutList.get(index).getAiResultId();// AI检验结果
cell = row.createCell(6);
cell.setCellValue(aiResultId);
String createdName = resutList.get(index).getCreatedName();// 扫描人员
cell = row.createCell(7);
cell.setCellValue(createdName);
Long createdAt = resutList.get(index).getCreatedAt();// 记录时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(createdAt);
cell = row.createCell(8);
cell.setCellValue(time);
}
}
//不同浏览器设置不同编码(未测试!)
public static String encodeFileName(HttpServletRequest request, String fileName)
throws UnsupportedEncodingException {
String newFilename = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");
String agent = request.getHeader("USER-AGENT").toLowerCase();
if (null != agent && -1 != agent.indexOf("msie")) {
/**
* IE浏览器,只能采用URLEncoder编码
*/
return newFilename;
} else if (null != agent && -1 != agent.indexOf("applewebkit")) {
/**
* Chrome浏览器,只能采用ISO编码的中文输出
*/
return new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else if (null != agent && -1 != agent.indexOf("opera")) {
/**
* Opera浏览器只可以使用filename*的中文输出
* RFC2231规定的标准
*/
return newFilename;
} else if (null != agent && -1 != agent.indexOf("safari")) {
/**
* Safani浏览器,只能采用iso编码的中文输出
*/
return new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else if (null != agent && -1 != agent.indexOf("firefox")) {
/**
* Firfox浏览器,可以使用filename*的中文输出
* RFC2231规定的标准
*/
return newFilename;
} else {
return newFilename;
}
}
本文转自:https://blog.csdn.net/wongrock/article/details/118359816
SpringBoot向Excel模板中写入数据并下载 (无需获取file对象及模板绝对路径)的更多相关文章
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- 复制excel表,往excel表中写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...
- java实现赋值excel模板,并在新文件中写入数据,并且下载
/** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...
- Python向excel中写入数据的方法 方法简单
最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- Java读取、写入、处理Excel文件中的数据(转载)
原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- 利用java反射机制实现读取excel表格中的数据
如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...
- oracle数据库中导入Excel表格中的数据
1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...
- 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型
导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...
随机推荐
- 授予用户/用户组访问 Kubernetes 的一个名称空间
转载地址:https://www.kuboard.cn/learning/k8s-advanced/sec/rbac/auth-namespace.html 前提条件 已安装 Kuboard v3,版 ...
- frps服务端与nginx可共用443端口
转载自: https://www.ioiox.com/archives/78.html frps服务器上的nginx frps.ini配置 由于nginx占用80/443端口,frps.ini中的 v ...
- NOIP2015 普及组 洛谷P2671 求和 (数学)
一道数学题...... 采用分组的思想,我们要统计答案的数对满足两个条件:同奇偶,同颜色.所以可以按这两个要求分组. 然后就是分组处理了,对于每组(有k个数),这里面的任意两对数都是满足条件的,可推出 ...
- CVPR2022 Oral OGM-GE阅读笔记
标题:Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR 2022 Oral) 论文:https://arxiv. ...
- Linux-->vi和vim编辑器的基本操作
vim编辑器介绍 vi或者vim就是对linux下的文本进行编辑的一种编辑器比如说a.cpp文件这种 Linux会内置vi文本编辑器 Vim可以简单的认为vi的增强版 Linux是区分大小写的! 用法 ...
- 微光互联 TX800-U 扫码器无法输出中文到光标的问题
问题背景 某检测场有一批扫码器,购于微光互联,型号 TX800-U,用于在不同办理窗口间扫描纸质材料上的二维码,简化录入过程.扫码器通过 USB 接入 PC 系统 (windows),自动安装驱动,接 ...
- Scanner的用法 从键盘输入
先导入包 import java.util.Scanner; 后输入 Scanner Sc=new Scanner(System.in); //(Sc可以自定义,无实质意义) int i; i=Sc. ...
- 微服务系列之分布式日志 ELK
1.ELK简介 ELK是ElasticSearch+LogStash+Kibana的缩写,是现代微服务架构流行的分布式日志解决方案,旨在大规模服务的日志集中管理查看,极大的为微服务开发人员提供了排查生 ...
- 齐博x1商业模块仅限一个国际域名使用
应用市场的所有商业模块 仅授权一个国际域名,大家不要试图复制到其它国际域名下使用. 仅支持一个国际域名使用,二级域名不限,但前提需要先用 www.开头的国际域名先安装,然后再到二级域名安装,并且二级域 ...
- 「浙江理工大学ACM入队200题系列」问题 E: 零基础学C/C++78——求奇数的乘积
本题是浙江理工大学ACM入队200题第八套中的E题 我们先来看一下这题的题面. 题面 输入 输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你 ...