/*
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.mpalourdio.hello.controllers;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Controller
public class PoiController {

    /**
     * The point here is to avoid injecting HttpServletResponse
     * because ResponseEntity has convenient methods to set
     * the http response caracteristics.
     *
     */

    @GetMapping(value = "/download", produces = "application/vnd.ms-excel")
    public ResponseEntity<byte[]> donwloadExcel() throws IOException {
        final HSSFWorkbook workbook = new HSSFWorkbook();
        final HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
        final HSSFRow row = worksheet.createRow(0);
        final HSSFCell cell = row.createCell(0);
        cell.setCellValue("cell value");

        final HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentDispositionFormData("attachment", "excel_file.xls");

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        outputStream.flush();

        return new ResponseEntity<>(outputStream.toByteArray(), responseHeaders, HttpStatus.OK);
    }
}

@@ -962,16 +962,63 @@
 pping;%0A%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import j
@@ -1047,16 +1047,16 @@
 Stream;%0A
-
 import j
@@ -1118,16 +1118,78 @@
 ller %7B%0A%0A
+    private static final String FILENAME = %22excel_file.xls%22;%0A%0A
     /**%0A
@@ -1357,15 +1357,8 @@
 cs.%0A
-     *%0A
     
@@ -1551,242 +1551,26 @@
 k = 
-new HSSFWorkbook();%0A        final HSSFSheet worksheet = workbook.createSheet(%22POI Worksheet%22);%0A        final HSSFRow row = worksheet.createRow(0);%0A        final HSSFCell cell = row.createCell(0);%0A        cell.setCellValue(%22cell value%22
+generateExcelFile(
 );%0A%0A
@@ -1911,63 +1911,425 @@
     
-return new ResponseEntity%3C%3E(outputStream.toByteArray(),
+final byte%5B%5D responseBody = outputStream.toByteArray();%0A        responseHeaders.setContentLength(responseBody.length);// useful ?%0A%0A        return new ResponseEntity%3C%3E(responseBody, responseHeaders, HttpStatus.OK);%0A    %7D%0A%0A    @GetMapping(value = %22/download2%22)%0A    public void donwloadExcelFile(final HttpServletResponse response) throws IOException %7B%0A        final HSSFWorkbook workbook = generateExcelFile();%0A%0A       
  res
@@ -2333,39 +2333,666 @@
 response
+.set
 Header
-s, HttpStatus.OK)
+(%22Content-Disposition%22, %22attachement; filename=%5C%22%22 + FILENAME + %22%5C%22%22);%0A%0A        //%22produces%22 in @GetMapping does not set the content-type with HttpServletResponse%0A        response.setHeader(%22Content-Type%22, %22application/vnd.ms-excel%22);%0A        workbook.write(response.getOutputStream());%0A    %7D%0A%0A    private HSSFWorkbook generateExcelFile() %7B%0A        final HSSFWorkbook workbook = new HSSFWorkbook();%0A        final HSSFSheet worksheet = workbook.createSheet(%22POI Worksheet%22);%0A        final HSSFRow row = worksheet.createRow(0);%0A        final HSSFCell cell = row.createCell(0);%0A        cell.setCellValue(%22cell value%22);%0A%0A        return workbook
 ;%0A    %7D%0A

Add example of file download with HttpServletResponse