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



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

无需获取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. C语言:类型存储

    类型存储 char: 可能为signed char 或 unsigned char ,根据编译器不同实现不同.占位一个字节.Signed char取值范围为-128~127, unsigned cha ...

  2. NSIS 检测默认浏览器

    #检测默认浏览器 #编写:水晶石 #原理:用FindExecutable函数查找htm关联程序路径与名称,然后分析字串中包含的可执行文件名. !include "LogicLib.nsh&q ...

  3. Codeforces Round #822 (Div. 2) A-F

    比赛链接 A 题解 知识点:贪心. 注意到任意三根木棍的相等最优解是最长减最小,因此从小到大排序,三个三个取,取最小值. 时间复杂度 \(O(n\log n)\) 空间复杂度 \(O(n)\) 代码 ...

  4. React魔法堂:echarts-for-react源码略读

    前言 在当前工业4.0和智能制造的产业升级浪潮当中,智慧大屏无疑是展示企业IT成果的最有效方式之一.然而其背后怎么能缺少ECharts的身影呢?对于React应用而言,直接使用ECharts并不是最高 ...

  5. 部署 LNMP(源码安装版本)shell脚本

    #!/bin/bash # 一键部署 LNMP(源码安装版本) menu() { clear echo " ##############‐‐‐‐Menu‐‐‐‐##############& ...

  6. for循环小九九乘法表

    for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { System.out.print(i+"*"+j+"=&quo ...

  7. js红宝书学习笔记(一)引用类型

    一.引用类型 ECMAScript中,引用类型是一种数据结构称之为对象定义,,引用对象不同于传统面向对象语言所支持的类和接口等基本结构 创建Object 实例的两种方式: new操作符跟Object构 ...

  8. Codeforces 1684 E. MEX vs DIFF

    题意 给你n个非负整数的数列a,你可以进行K次操作,每次操作可以将任意位置的数数更改成任意一个非负整数,求操作以后,DIFF(a)-MEX(a)的最小值:DIFF代表数组中数的种类.MEX代表数组中未 ...

  9. 【.NET 6】RabbitMQ延迟消费指南

    背景 最近遇到一个比较特殊需求,需要修改一个的RabbitMQ消费者,以实现在消费某种特定的类型消息时,延迟1小时再处理,几个需要注意的点: 延迟是以小时为单位 不是所有消息都延迟消费,只延迟特定类型 ...

  10. .NET 6学习笔记(4)——如何在.NET 6的Desktop App中使用Windows Runtime API

    Windows Runtime API是当初某软为了区别Win32 API,力挺UWP而创建的另一套Windows 10专用的API集合.后来因为一些原因,UWP没火.为了不埋没很有价值的Window ...