title: Trouble exporting to excel from mvc action
body: <p>I created a simple action to download some content as excel file:</p>
  
  <pre><code>public FileResult ExportToExcel()
      {
          string filename = "list.xlsx";
          string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  
          List&lt;string[]&gt; list = new List&lt;string[]&gt;();
          list.Add(new[] { "col1", "col2", "cols3" });
          list.Add(new[] { "col4", "col5", "cols6" });
          list.Add(new[] { "col7", "col8", "cols9" });
  
          StringWriter sw = new StringWriter();
  
          sw.WriteLine("ID,Date,Description");
  
          foreach (string[] item in list)
          {
              sw.WriteLine("{0},{1},{2}", item[0], item[1], item[2]);
          }
  
          byte[] fileContents = Encoding.UTF8.GetBytes(sw.ToString());
  
          return this.File(fileContents, contentType, filename);
      }
  </code></pre>
  
  <p>I have 2 issues with it:</p>
  
  <p><strong>1.</strong> The file is downloaded but I cannot open it and am getting a warning:</p>
  
  <p>Excel cannot open the file ... because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.</p>
  
  <p>When I use old excel format:</p>
  
  <pre><code>        string filename = "List.xls";
          string contentType = "application/vnd.ms-excel";
  </code></pre>
  
  <p>I am able to open the file but after 3 different warnings about file being corrupted etc.</p>
  
  <p>Btw I compared saving and tried to write file as pdf</p>
  
  <pre><code>        string filename = "List.pdf";
          string contentType = "application/pdf";
  </code></pre>
  
  <p>And I still couldn't open the file - it said format is not valid etc.</p>
  
  <p><strong>2.</strong> The contents appear in the file in the second example however the commas are not recognised as column separators and all data in a row is written as one column.</p>
  
  <p>What separator to use for excel format or how to write data to file to have it in a table excel format?</p>
  
  <p>Ideal solution for me would be just return exported view (strongly typed) but I didn't find out how to do it so far.</p>
  
  <p><strong>--- EDIT: Working solution ---</strong></p>
  
  <pre><code>public FileResult ExportToExcel()
      {
          string filename = "List.xlsx";
          string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  
          List&lt;string[]&gt; titles = new List&lt;string[]&gt;() { new[] { "a", "be", "ce" } };
          List&lt;string[]&gt; list = new List&lt;string[]&gt;
                                    {
                                        new[] { "col1", "col2", "cols3" },
                                        new[] { "col4", "col5", "cols6" },
                                        new[] { "col7", "col8", "cols9" },
                                        new[] { "col10", "col11", "cols12" }
                                    };
  
          XLWorkbook wb = new XLWorkbook();
          XLTables xt = new XLTables();
          var ws = wb.Worksheets.Add("List");
          ws.Cell(1, 1).InsertData(titles);
          ws.Cell(2, 1).InsertData(list);
          ws.Columns().AdjustToContents();
  
          var stream = new MemoryStream();
          wb.SaveAs(stream);
          stream.Seek(0, SeekOrigin.Begin);
          wb.Dispose();
  
          return this.File(stream, contentType, filename);
      }
  </code></pre>