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");
        }
1
  • 2
    Duplicate Commented Jun 22, 2016 at 17:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.