How to display Chinese garbled characters in Excel exported by asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows: protected void btnExcel_Click (object sender, EventArgs e)
{
List <bUFlow. Model. orderhistory> orderlist = dal. GetOrderList2 ("");
String filename = "3G traffic network age Upgrade Plan User table ";
String name1 = filename;
String bname = Common. MyRequest. GetBrowserName (). ToLower ();
If (bname. Contains ("firefox "))
{
}
Else if (bname. Contains ("ie "))
{
Filename = HttpUtility. UrlEncode (filename, System. Text. Encoding. UTF8 );
}
Else
{
Filename = HttpUtility. UrlEncode (filename, System. Text. Encoding. UTF8 );
}

HttpResponse resp = System. Web. HttpContext. Current. Response;
Resp. Charset = "UTF-8 ";
Resp. Clear ();
Resp. AppendHeader ("Content-Disposition", "attachment; filename =" + filename + ". xls ");
Resp. ContentEncoding = System. Text. Encoding. UTF8;

Resp. ContentType = "application/ms-excel ";
String style = "<meta http-equiv = \" content-type \ "content = \" application/ms-excel; charset = UTF-8 \ "/>" + "<style>. table {font: 9pt Tahoma, Verdana; color: #000000; text-align: center; background-color: #8ECBE8 ;}. table td {text-align: center; height: 21px; background-color: # EFF6FF ;}. table th {font: 9pt Tahoma, Verdana; color: #000000; font-weight: bold; background-color: #8 ECBEA; height: 25px; text-align: center; padding-left: 10px ;}</style> ";
Resp. Write (style );
// Resp. Write (ExportTable (list ));
Resp. write ("<table class = 'table'> <tr> <th>" + "Mobile Phone" + "</th> <th>" + "channel" + "</ th> <th> "+" time "+" </th> </tr> ");
// DbVideoList = (List <subShiyongModel>) Session ["dbVideoList"];
Foreach (bUFlow. Model. orderhistory model in orderlist)
{
Resp. Write ("<tr> <td>" + model. phone + "</td> ");
Resp. Write ("<td>" + model. qudao + "</td> ");
Resp. Write ("<td>" + model. tm + "</td> ");
Resp. Write ("</tr> ");
}
Resp. Write ("<table> ");

Resp. Flush ();
Resp. End ();
}

Note the encoding problem. It is best to add the following statement in the output:Copy codeThe Code is as follows: <meta http-equiv = "content-type" content = "application/ms-excel; charset = gb2312"/>

Related Article

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.