2015-04-27 145 views
0

我是用下面的代码编写的,用于下载xls或xlsx。我可以下载xls或xlsx文件,但它说文件已损坏。 有人来帮我处理这种错误如何使用apache POI下载xls或xlsx文件

public static void writeUploadErrorDetailsToExcel(List<OfflineRegistrationBean> userErrorList, 
      HttpServletResponse response, String uploadedfileName) 
{ 
    Workbook workbook = null; 
    response.reset(); 
    if (StringUtils.hasText(uploadedfileName) && uploadedfileName.contains(".xlsx")) 
    { 
     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
     workbook = new XSSFWorkbook(); 
    } 
    else 
    { 
     response.setContentType("application/vnd.ms-excel"); 
     workbook = new HSSFWorkbook(); 
    } 

    Sheet offlineErrorMsg = workbook.createSheet("offlineLeads_error"); 
    int rowIndex = 0; 
    if (userErrorList != null && userErrorList.size() > 0) 
    { 
     String sheeHead[] = IConstants.SHEET_HEADING.split("##"); 
     for (int i = 0; i < sheeHead.length; i++) 
     { 
      Row row = offlineErrorMsg.createRow(rowIndex++); 
      row.createCell(i).setCellValue(sheeHead[i]); 
     } 
     for (OfflineRegistrationBean registrationBean : userErrorList) 
     { 
      Row row = offlineErrorMsg.createRow(rowIndex++); 
      int cellIndex = 0; 
      row.createCell(cellIndex++).setCellValue(registrationBean.getFirstName()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getLastName()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getEmail()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getPhoneNo()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getStudyLevel()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getYearValue()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getOffice()); 
      row.createCell(cellIndex++).setCellValue(registrationBean.getErrorMessage()); 
     } 

     try 
     { 
      response.setHeader("content-disposition", "attachment; filename="+uploadedfileName); 
      OutputStream outObject = response.getOutputStream(); 
      workbook.write(outObject);    
      outObject.flush(); 
      outObject.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

有没有办法可以达致这只能使用Apache POI库,你甲肝ETO使用Apache的百科全书io的对于相同的。除了代码之外,唯一的事情就是将文件写入输出流。下面的代码应该可以帮助你下载文件。您面临的问题是因为您只将工作簿写入输出流,但服务器无法将响应作为文件发送,因此要做到这一点,需要将输出流写入缓冲区。

OutputStream outStream = response.getOutputStream(); 

    byte[] buffer = new byte[4096]; 
    int bytesRead = -1; 

    while ((bytesRead = inStream.read(buffer)) != -1) { 
     outStream.write(buffer, 0, bytesRead); 
    } 
0

您不需要区分xsl和xlsx。

public static GTCSExcel create(InputStream inputStream){ 
     Workbook workbook = null; 
     try { 
      workbook = WorkbookFactory.create(inputStream); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (InvalidFormatException e) { 
      e.printStackTrace(); 
     } 
     return new GTCSExcel(workbook); 
    } 

你可以用这个〜

相关问题