I have a web page with a gridview control and a button for Exporting to Excel
I get following error when exporting to Excel.
"The fiel you are trying to open, 'someFileName.xls, is in a different format than specified by the file extension. Verify that file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
Clicking Yes opens the file. I would like to avoid showing the above error to the users.
Following code is used from the click event. I've left the commented code here to show what I've tried based on results from internet search. I have Microsoft Office Excel 2007 on local PC.
public static void ToExcel(DataTable dt, string fileName)
string attachment = "attachment; filename=" + fileName + ".xls";
GridView gvExcel = new System.Web.UI.WebControls.GridView();
gvExcel.AutoGenerateColumns = true;
gvExcel.DataSource = dt;
//HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
View Complete Post