簡體   English   中英

如何使用apache POI下載xls或xlsx文件

[英]How to download the xls or xlsx file using apache POI

我編寫了以下用於下載xls或xlsx的代碼。 我可以下載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();
        }
    }
}

僅使用apache POI庫就無法實現此目的,而您也要使用apache commons io。 除了代碼外,唯一的事情就是將文件寫入輸出流。 下面的代碼應該可以幫助您下​​載文件。 您面臨的問題是,因為您僅將工作簿寫入輸出流,但是服務器無法將響應作為文件發送,因此您需要將輸出流寫入緩沖區。

OutputStream outStream = response.getOutputStream();

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

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

您無需區分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);
    }

你可以用這個〜

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM