0

I am using Excel 2010 I have the following code that I like to export to a xls file but I am getting the following message:

The file you are trying to open, 'Report.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is for the trusted source before opening the file. Do you want to open the file now?

Note that is still opens but not sure why I am getting the message

           ....
            a.Fill(dtRecords);

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.ContentType = "application/ms-excel";
            string tab = string.Empty;

            foreach (DataColumn datacol in dtRecords.Columns)
            {
                Response.Write(tab + datacol.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");

            foreach (DataRow dr in dtRecords.Rows)
            {
                tab = "";
                for (int j = 0; j < dtRecords.Columns.Count; j++)
                {
                    Response.Write(tab + Convert.ToString(dr[j]));
                    tab = "\t";
                }

                Response.Write("\n");
            }
            Response.End();

2 Answers 2

1

I believe the official mime type for .xls files is application/vnd.ms-excel (source)

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

Comments

1

You are getting this message because you are trying to export character separated value (CSV) stream as XLS file.

CSV is textual format, while XLS is complex binary format with much more features than CSV.

MS Excel complains because it first tried to open the file as XLS but it figured out that file is actually not XLS so it shows warning message, but is still smart enough to figure out that file is CSV and act accordingly.

Try saving the file with extension .csv and with content-type text/csv.

1 Comment

Excel 2007 and earlier versions didn't provide this warning when opening a file with a .XLS extenstion that was actually formatted as a comma separated values (CSV) or tab separated values (TSV). The warning started with Excel 2010, so users with older versions of Excel won't get this warning message.

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.