之前用获取模板路径的方式测试没问题打包后就有问题了



莫名出现一个! 找了很多教程尝试无果 最终使用下面这个方式

无需获取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对象及模板绝对路径)的更多相关文章

  1. POI往word模板中写入数据

    转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977  版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  2. 复制excel表,往excel表中写入数据

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...

  3. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

  4. Python向excel中写入数据的方法 方法简单

    最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...

  5. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  6. Java读取、写入、处理Excel文件中的数据(转载)

    原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...

  7. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  8. 利用java反射机制实现读取excel表格中的数据

    如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...

  9. oracle数据库中导入Excel表格中的数据

    1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...

  10. 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型

    导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...

随机推荐

  1. 【前端必会】NVM,管理你的node版本

    介绍 用nvm管理node,可以随时修改node版本 使用 下载nvm https://github.com/coreybutler/nvm-windows/releases/tag/1.1.9 安装 ...

  2. HashMap底层原理及jdk1.8源码解读

    一.前言 写在前面:小编码字收集资料花了一天的时间整理出来,对你有帮助一键三连走一波哈,谢谢啦!! HashMap在我们日常开发中可谓经常遇到,HashMap 源码和底层原理在现在面试中是必问的.所以 ...

  3. PAT520 钻石争霸赛 7-6 随机输一次

    #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1000; ll n ...

  4. 关于JDK8中stream的用法小总结。

    import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; public class Ma ...

  5. .NET MAUI 社区工具包 1.3版本发布

    2022 年 10 月 4 日,微软发布了 .NET MAUI 社区工具包的 1.3 版,具体参见微软官方博客:https://devblogs.microsoft.com/dotnet/announ ...

  6. git记不住用户名跟密码,每次提交拉取都需要再次输入

    问题:之前为了测试git提交的一个问题,选择不记住用户名跟密码,输入如下命令即可不记住 git credential-manager uninstall git update-git-for-wind ...

  7. 1.RabbitMQ系列之服务启动

    1. docker方式启动MQ # latest RabbitMQ 3.10 docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:156 ...

  8. 1、在SrpingBoot的环境当中使用JSP及相关功能

    创建webapp目录 由于SpringBoot项目不建议直接访问jsp页面,但是我现在要做的事情需要去访问,那么我就需要在原有的项目基础上为访问jsp页面进行一个调整 首先在项目当中,java和res ...

  9. 知识图谱-生物信息学-医学顶刊论文(Bioinformatics-2021)-SumGNN:通过有效的KG聚集进行多类型DDI预测

    3.(2021.3.26)Bioinformatics-SumGNN:通过有效的KG聚集进行多类型DDI预测 论文标题: SumGNN: multi-typed drug interaction pr ...

  10. python关于Django搭建简单博客项目 详解二-setting.py

    这一篇我们来讲解setting.py,具体内容以注释形式写入到下面的setting.py代码中,篇幅所限已把官方所给英文注释删除. 全部源代码和详解请参看http://github.com/Cheng ...