How can I troubleshoot Chinese garbled characters when I import data from a gridview to an Excel file?

Source: Internet
Author: User

To export an Excel file in Asp.net, follow these steps:

Response. charset = "gb2312 ";

Response. addheader ("content-disposition", "attachment; filename =" Your httputility.urlencode( .xls ""));

Response. contenttype = "application/MS-excel ";

However, when there are Chinese characters, garbled characters often occur, and there are many solutions, but they cannot be comprehensively solved.

HoweverCodeAfter making the following changes:

Response. Write ("<HTML>

System. Io. stringwriter Sw = new system. Io. stringwriter ();
Htmltextwriter hW = new htmltextwriter (SW );
This. gridview1.rendercontrol (HW );
Response. Write (SW. tostring ());
Response. Write ("</body>

In this way, the problem is solved.

I guess that when I read UTF-8 in Excel, I will change the character set reading method,

However, the original garbled characters appear again when he is changed to Unicode.

It is for reference only. If you have any questions, you can learn from each other !!!

 Response. Clear ();
Response. charset = " Gb2312 " ;
Response. Buffer = True ;
Response. addheader ( " Content-Disposition " , " Attachment; filename = " + Httputility. urlencode ( " User information table .xls " ). Tostring ());
Response. contenttype = " Application/MS-Excel " ;
Response. Write ( " <HTML> " );
System. Io. stringwriter SW = New System. Io. stringwriter ();
Htmltextwriter HW = New Htmltextwriter (SW );
This . Gridview1.rendercontrol (HW );
Response. Write (SW. tostring ());
Response. Write ( " </Body> " );
Response. Flush ();
Response. End ();

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.