Copy Code code as follows:
protected void Excel_click (object sender, EventArgs e)
{
Response.Charset = "UTF-8";
Response.clearcontent ();
Response.Clear ();
response.contentencoding = System.Text.Encoding.UTF8;
response.headerencoding = System.Text.Encoding.UTF8;
Response.AddHeader ("Content-disposition", "attachment; Filename=myexpress.xls ");
Response.ContentType = "Application/excel";
System.IO.StringWriter SW = new System.IO.StringWriter ();
HtmlTextWriter HTW = new HtmlTextWriter (SW);
Turn off paging
Gridview1.allowpaging = false;
DataBind ();
Gridview1.rendercontrol (HTW);
Response.Write (SW. ToString ());
Response.End ();
Turn the paging on again
Gridview1.allowpaging = true;
DataBind ();
}
The essential:
Copy Code code as follows:
Response.Charset = "UTF-8";//Add Encoding format
Response.clearcontent ();
Response.Clear ();
response.contentencoding = system.text.encoding.utf8;//table content Add encoding format
response.headerencoding = system.text.encoding.utf8;//table header Add encoding format
If it doesn't work out, it can be used.
Copy Code code as follows:
Response.clearcontent ();
Response.Clear ();
Response.AddHeader ("Content-disposition", "attachment; Filename=sumlate.xls ");
Response.Charset = "GB2312";
response.contentencoding = System.Text.Encoding.GetEncoding ("GB2312");
Response.ContentType = "Application/excel";
System.IO.StringWriter SW = new System.IO.StringWriter ();
HtmlTextWriter HTW = new HtmlTextWriter (SW);
if (GridView2.Rows.Count > 0)
{
Gridview2.rendercontrol (HTW);
}
Else
{
Gridview1.rendercontrol (HTW);
}
Response.Write (SW. ToString ());
Response.End ();
The essential:
Copy Code code as follows:
Response.Charset = "GB2312";
response.contentencoding = System.Text.Encoding.GetEncoding ("GB2312");
Attention to observation, the main reason is actually coding format problem.
Now you can prevent the export of garbled problems