This is my code, but I end up with an Unexpected format error. Something must be wrong with the MIME-type, however I found this one as being te original official MIME-type.
What am I missing? Each time I try to open any document I create by this method seems to be "corrupt", but when I click the message away everything works fine.
Message: The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
However, I need to get rid of this warning, since I cannot import the file because it's invalid. If I save the file as 2003-2007 format .xls, it seems to be fixed. But this is not a working solution.
protected void ExportToExcel(string fileName)
{
var cmd = new SqlCommand(query);
var dt = GetData(cmd);
var writer = new StringWriter();
var gridview = new GridView();
gridview.AllowPaging = false;
gridview.DataSource = dt;
gridview.DataBind();
Response.Clear();
var hw = new HtmlTextWriter(writer);
gridview.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
// this.EnableViewState = false;
Response.Write(writer.ToString());
// Response.Flush();
Response.End();
}