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 ...
随机推荐
- 使用FastDFS打造一款高可用的分布式文件系统
FastDFS 介绍 参考: http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文 ...
- Dubbo 02: 直连式
直连式 需要用到两个相互独立的maven的web项目 项目1:o1-link-userservice-provider 作为服务的提供者 项目2:o2-link-consumer 作为使用服务的消费者 ...
- paddle&蜜度 文本智能较对大赛经验分享(17/685)
引言 我之前参加了一个中文文本智能校对大赛,拿了17名,虽然没什么奖金但好歹也是自己solo拿的第一个比较好的名次吧,期间也学到了一些BERT应用的新视角和新的预训练方法,感觉还挺有趣的,所以在这里记 ...
- Vue学习之--------Vue生命周期beforeCreate、created、beforeMount、mounted、beforeDestroy 。。。(图解详细过程)(2022/7/17)
文章目录 1.Vue生命周期 1.1 概念 1.2 图解 2.钩子函数的用法说明 2.1 beforeCreate()和created()的详细讲述 2.1.1 方法说明 2.1.2 代码实例 2.1 ...
- 自建流媒体如何录制视频。齐博x1齐博x2齐博x3齐博x4齐博x5齐博x6齐博x7齐博x8齐博x9齐博x10
http://x1.eapis.site/ 先打开配置文件\conf\config.php 里边的内容大概如下,第一项是必须要配置的,换成你的网站域名网址.第二项,如果流媒体服务器配置了https证书 ...
- 记一次 .NET 某医疗器械 程序崩溃分析
一:背景 1.讲故事 前段时间有位朋友在微信上找到我,说他的程序偶发性崩溃,让我帮忙看下怎么回事,上面给的压力比较大,对于这种偶发性崩溃,比较好的办法就是利用 AEDebug 在程序崩溃的时候自动抽一 ...
- Spring Cloud 整合 nacos 实现动态配置中心
上一篇文章讲解了Spring Cloud 整合 nacos 实现服务注册与发现,nacos除了有服务注册与发现的功能,还有提供动态配置服务的功能.本文主要讲解Spring Cloud 整合nacos实 ...
- VideoPipe可视化视频结构化框架新增功能详解(2022-11-4)
VideoPipe从国庆节上线源代码到现在经历过了一个月时间,期间吸引了若干小伙伴的参与,现将本阶段新增内容总结如下,有兴趣的朋友可以加微信拉群交流. 项目地址:https://github.com/ ...
- FastApi学习1
先写路由文件: 其次通过ORM操作数据库相关:
- java反序列化cc_link_one2
CC-LINK-one_second 前言 这条链子其实是上一条链子的另一种走法,在调用危险函数哪里是没有什么变化的 整体链子 还是尾部没有变化嘛还是InvokerTransformer的transf ...