1

When I download excel using this code I am getting the warning. "The file type you are trying to open is in a different format...." How to avoid this? enter image description here

 public FileResult ExportExcel()
        {
            DataTable  dt = new DataTable();
            DataRow dr = dt.NewRow();
            dt.Columns.Add("name");
            dr["name"] = "test";
            dt.Rows.Add(dr);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            string csv_text = ToCSV(dt);
            byte[] toBytes = Encoding.ASCII.GetBytes(csv_text);

            return File(toBytes, "application/ms-excel", "mytestfile.xls");
        }
  • 2
    [Duplicate](http://stackoverflow.com/questions/16144900/the-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-f) – Stacked Jun 22 '16 at 17:19

1 Answers1

1

What you're trying to download is a just CSV file, not an Excel spreadsheet, try changing the return statement to:

return File(toBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "mytestfile.csv");

If you need to create an Excel doc, use something like ExcelLibrary

Alan Donnelly
  • 79
  • 1
  • 3