java - 作为 InputStreamResource 返回时 Excel 文件未打开
问题描述
我正在尝试创建一个发送 Excel 文件的 InputStreamResource 的 API。它是在服务器端从表的数据创建的。
当我从邮递员那里点击这个 api 时,excel 文件没有打开,并说它没有正确的扩展名或文件已损坏。
我已将文件保存在服务器端,可以正常打开。
也尝试发送 HTTP ENTITY。仍然得到相同的回应
这是我的控制器
@GetMapping(path = RestMappingConstants.AdminRequestUri.DOWNLOAD_A2TO_INTERVIEWER_EXCEL)
public ResponseEntity<InputStreamResource> downloadA2ToInterviewPaymentExcel( ) throws IOException{
ByteArrayInputStream in = a2AdminService.downloadA2ToInterviewPaymentExcel();
HttpHeaders headers = new HttpHeaders();
//InputStream res=A2AdminController.class.getClassLoader().getResourceAsStream("payment.xlsx");
headers.add("Content-Disposition", "attachment; filename=PaymentDetails.xlsx");
headers.setContentType(new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
return ResponseEntity
.ok()
.headers(headers)
.body(new InputStreamResource(in));
}
这是我的服务班
@Override
public ByteArrayInputStream downloadA2ToInterviewPaymentExcel() throws IOException {
List<A2ToInterviewerPaymentEntity> a2ToInterviewerPaymentEntityList = a2AdminDao.getApprovedPaymentList();
// FileOutputStream file=new FileOutputStream("payment.xlsx");
// convert to excel
String[] columns = { "Id", "Amount", "PaymentMode", "PaymentType", "TransactionId", "InterviewScheduleId",
"EmployerToPaymentEntityId", "PaymentStatus" };
try (Workbook workbook = new XSSFWorkbook(); ByteArrayOutputStream out = new ByteArrayOutputStream();) {
Sheet sheet = workbook.createSheet("payment");
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.BLUE.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
// Row for Header
Row headerRow = sheet.createRow(0);
// Header
for (int col = 0; col < columns.length; col++) {
Cell cell = headerRow.createCell(col);
cell.setCellValue(columns[col]);
cell.setCellStyle(headerCellStyle);
}
int rowIdx = 1;
for (A2ToInterviewerPaymentEntity paymentEntity : a2ToInterviewerPaymentEntityList) {
Row row = sheet.createRow(rowIdx++);
row.createCell(0).setCellValue(paymentEntity.getId());
row.createCell(1).setCellValue(paymentEntity.getAmount());
row.createCell(2).setCellValue("UPI");
row.createCell(3).setCellValue("TRANSFER");
row.createCell(4).setCellValue(paymentEntity.getTransactionId());
row.createCell(5).setCellValue(paymentEntity.getInterviewSchedule().getId());
row.createCell(6).setCellValue(paymentEntity.getEmployerToA2PaymentEntity().getId());
row.createCell(7).setCellValue(paymentEntity.getPaymentStatus().toString());
}
workbook.write(out);
// workbook.write(file);
byte[] arr = out.toByteArray();out.flush();out.close();
return new ByteArrayInputStream(arr);
}
}
解决方案
尝试在您的控制器中使用此代码:
ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" +yourFileName".xlsx")
.body(yourFileInByteArray);
推荐阅读
- c - 使用双指针迭代结构数组并使用双指针访问元素
- python - 使用 Django-two-factor-auth 将用户标记为已登录
- python - discord.py 中的城市字典 API
- yaml - 这在 YAML 中意味着什么?
- python - 安装 PyTorch 时遇到问题
- python - 抽象方法是否应该有主体?因为在某些网站上它说“是”而其他网站说“不是”?
- java - OnMarkerClick() 方法不起作用 Android Studio
- reactjs - TypeError:无法读取未定义的属性“byteLength”-AWS S3 上传
- mysql - 当结果很少时 MySQL-select 查询很慢
- sql-server - 使用远程服务器上的 SAS 使用 Microsoft NT 身份验证将 SAS 连接到 SQL Server 数据库 ODBC