asp.net reasons for exporting Excel garbled and solutions _ practical skills

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.