title: Return Excel document from Spring Controller using Apache POI
body: <p>I am trying to send a Excel file back to the user. In some way my excel file gets corrupted even if it is empty. When I open the file Excel tells me that the file is corrupt. </p>
  
  <p>If i remove  </p>
  
  <pre><code>response.getOutputStream().write(excelService.exportEventsToCSV());
  </code></pre>
  
  <p>I get an empty excel file. </p>
  
  <pre><code>@RequestMapping(value = "/events/excel", method = RequestMethod.GET)
      public void getEventsAsExcel(HttpServletResponse response) {
  
          try {
  
              response.setHeader("Content-disposition", "attachment; filename=test.xls");
              response.getOutputStream().write(excelService.exportEventsToCSV());
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>My XML generation is very simple. Just empty document </p>
  
  <pre><code>@Override
  public byte[] exportEventsToCSV() {
      try {
  
          HSSFWorkbook workbook = new HSSFWorkbook();
          HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
          HSSFCellStyle cellStyle = workbook.createCellStyle();
  
  
          workbook.write(fileOut);
  
          return workbook.getBytes();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Please any advice. I Was thinking of Springs Excel view but I dont want a view. Just a downloaded file.</p>