简体   繁体   中英

How to download the xls or xlsx file using apache POI

I am written following code for downloading xls or xlsx. I can able to downloaded the xls or xlsx file, but it's say file was corrupted. Someone has to help me for fixing this bug

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();
        }
    }
}

There is no way you can acheive this only using the apache POI library, you hav eto use apache commons io for the same. The only thing apart from your code is to write the file to the outputstream. Below code should help you to download the file. The problem your are facing is because you are only writing the workbook to the output stream but there is no way for the server to send the response as file, so to do that you need to write the outputstream to the buffer.

OutputStream outStream = response.getOutputStream();

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

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

you do not need to distinguish between xsl and 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);
    }

you can use this~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM