繁体   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