首页 > 解决方案 > 作为 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);
    }
}

标签: javaspringrestspring-bootfile

解决方案


尝试在您的控制器中使用此代码:

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

推荐阅读