Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello friends.... im exprting an excel file in c# an my code is below... its downloading successfully . and when opening in ms excel , it shows a message that 'the file format different than specified by the file extension. how to prevent this message in c# (when downloading the file ). is there any solution? please guide . thanks in advance .

protected void Button1_Click1(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attatchment;filename={0}", "Dispatched_delivery_sample.xls"));
Response.Charset = "";
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.HeaderStyle.BackColor = System.Drawing.Color.Orange;
for (int a = 0; a < GridView1.Rows.Count; a++)
{
GridViewRow row = GridView1.Rows[a];


}
GridView1.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.End();
}
catch
{


}
}
Posted
Comments
Sergey Alexandrovich Kryukov 22-Dec-15 1:44am    
Try with empty catch is just a crime; against yourself, I guess. This is blocking of exception propagation. Better don't handle exceptions at all, don't handle locally.
—SA
Ashutosh shukla207 24-Dec-15 6:52am    
okk sir ... i will remember .
CHill60 22-Dec-15 3:34am    
It may be that the file is being downloaded with a ".xlsx" extension but is actually a ".xls", or a ".csv" ... look at the file and make sure the extension is correct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900