Search code examples
javastrutsapache-poi

POI not writing to excel


Im using POI to write report to excel. This is my code:

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Export To Excel");     

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=avt.xls");

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("AAAAAAAAAAAAAA");
        cell = row.createCell(1);
        cell.setCellValue("BBBBB");
//        cell.setCellStyle(cellStyle);




        FileOutputStream out =  new FileOutputStream("avt.xls");    
        wb.write(out);     
        out.close();     System.out.println("Excel written successfully..");  

When I click the button the excel popup opens with a warning message but nothing gets written to0 the excel. i don't see any error in the compiler too. whats wrong>?


Solution

  • FileOutputStream tries to write to a file on the server side. If you are trying to write it with response, then you should use the outputstream of the response.

    wb.write(response.getOutputStream());
    response.getOutputStream().close();