title: Spring boot - downloading xlsx file - cannot open
body: <p>I'm trying to create an endpoint that returns xlsx file to download.</p>
  <p>When I'm trying to open this generated file with OpenOffice then everything works fine.
  It doesn't work with Microsoft Excel, when trying to open it shows something like &quot;excel cannot open the file due to invalid format&quot;.</p>
  <p>Unfortunatelly I have no idea why is that.
  I did try (as you can see in the code snippet) different content types.</p>
  <p>This is sample of my code:</p>
  <pre class="lang-java prettyprint-override"><code>    package com.example.demo;
  
      import org.apache.poi.ss.usermodel.Cell;
      import org.apache.poi.ss.usermodel.Row;
      import org.apache.poi.ss.usermodel.Sheet;
      import org.apache.poi.ss.usermodel.Workbook;
      import org.apache.poi.xssf.usermodel.XSSFWorkbook;
      import org.springframework.http.*;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
  
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
  
      @RestController
      @RequestMapping(&quot;/xls&quot;)
      public class XlsController {
  
          @GetMapping
          public ResponseEntity&lt;byte[]&gt; get() {
              try {
                  final byte[] data = getData();
                  String fileName = &quot;test.xlsx&quot;;
  
                  HttpHeaders header = new HttpHeaders();
                  header.setContentLength(data.length);
      //            header.setContentType(MediaType.parseMediaType(&quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8&quot;));
      //            header.setContentType(MediaType.parseMediaType(&quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;));
                  header.setContentType(MediaType.parseMediaType(&quot;application/vnd.ms-excel&quot;));
                  header.setCacheControl(CacheControl.noCache());
                  header.set(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=&quot; + fileName);
  
                  return new ResponseEntity&lt;&gt;(data, header, HttpStatus.OK);
              } catch (Exception e) {
                  return new ResponseEntity&lt;&gt;(null, HttpStatus.INTERNAL_SERVER_ERROR);
              }
          }
  
          private byte[] getData() throws IOException {
  
              String[][] data = {
                      {&quot;A1&quot;, &quot;B1&quot;, &quot;C1&quot;, &quot;D1&quot;},
                      {&quot;A2&quot;, &quot;B2&quot;, &quot;C2&quot;, &quot;D2&quot;}
              };
  
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
              try (Workbook workbook = new XSSFWorkbook()) {
                  int rowNumber = 0;
                  int totalNumberOfColumns = 4;
                  int totalNumberOfRows = 2;
  
                  Sheet sheet = workbook.createSheet(&quot;Test Sheet&quot;);
                  Row row = sheet.createRow(rowNumber);
                  Cell cell;
  
                  for (int i = 0; i &lt; totalNumberOfRows; i++) {
                      for (int j = 0; j &lt; totalNumberOfColumns; j++) {
                          cell = row.createCell(i);
                          cell.setCellValue(data[i][j]);
                      }
                  }
  
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  baos.close();
              }
  
              return baos.toByteArray();
          }
      }
  </code></pre>
  <p>Thanks in advance.</p>