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



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

无需获取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. jenkins邮箱配置

  2. 打印 Logger 日志时,需不需要再封装一下工具类?

    在开发过程中,打印日志是必不可少的,因为日志关乎于应用的问题排查.应用监控等.现在打印日志一般都是使用 slf4j,因为使用日志门面,有助于打印方式统一,即使后面更换日志框架,也非常方便.在 < ...

  3. 这些不知道,别说你熟悉 Spring

    大家好,这篇文章跟大家来聊下 Spring 中提供的常用扩展点.Spring SPI 机制.以及 SpringBoot 自动装配原理,重点介绍下 Spring 基于这些扩展点怎么跟配置中心(Apoll ...

  4. Go中的闭包、递归

    一 闭包详解 闭包的应该都听过,但到底什么是闭包呢? 闭包是由函数及其相关引用环境组合而成的实体(即:闭包=函数+引用环境). "官方"的解释是:所谓"闭包", ...

  5. Linux文本相关命令

    Linux文本相关命令 目录 Linux文本相关命令 文本排序命令 文本去重命令 基础命令cut 文本三剑客 sed awk grep 文本排序命令 sort 常用参数: -n:以数值大小进行排序 - ...

  6. 微信电脑版DAT文件转图片工具

    一键批量将微信聊天接受到的加密存储DAT图片文件转化为普通图片. 通过查看转化后的图片,您可以: (1)清理无用的历史图片,节省电脑硬盘存储空间. (2)恢复寻找重要照片资料. 下载地址:点此下载 微 ...

  7. Pytest进阶使用

    fixture 特点: 命令灵活:对于setup,teardown可以省略 数据共享:在conftest.py配置里写方法可以实现数据共享,不需要import导入,可以跨文件共享 scope的层次及神 ...

  8. 2、第二种传输数据的形式:使用ajax传输数据,将前台的数据传输到后端

    第一种使用form表单中的action形式传输数据:https://blog.csdn.net/weixin_43304253/article/details/120335282 前端页面 <% ...

  9. grpc错误处理

    0.1.索引 https://waterflow.link/articles/1665938704477 我们都知道当发起http请求的时候,服务端会返回一些http状态码,不管是成功还是失败.客户端 ...

  10. import cv2报错

    其实是没错的,不过有的python编译器对这个不太支持,把import cv2 改为import cv2.cv2 as cv2就行了.