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

时间:2015-04-27 03:38:45

标签: java servlets 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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你无法使用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);
    }

答案 1 :(得分: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);
    }

你可以用这个〜