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 ();