2015-04-27 166 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); 
    } 

你可以用這個〜

相關問題