gpt4 book ai didi

java - 作为 InputStreamResource 返回时 Excel 文件未打开

转载 作者:行者123 更新时间:2023-12-01 22:53:53 26 4
gpt4 key购买 nike

我正在尝试创建一个发送 Excel 文件的 InputStreamResource 的 API。它是在服务器端根据表的数据创建的。

当我从 postman 那里点击这个 api 时,excel 文件没有打开,并说它没有正确的扩展名或文件已损坏。

我已将文件保存在服务器端,该文件可以正常打开。

也尝试过发送 HTTP ENTITY。仍然得到相同的回应

这是我的 Controller

@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);
}
}

最佳答案

尝试在 Controller 中使用以下代码:

ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" +yourFileName".xlsx")
.body(yourFileInByteArray);

关于java - 作为 InputStreamResource 返回时 Excel 文件未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58448286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com