asp.net匯出Excel顯示中文亂碼的解決方案

來源:互聯網
上載者:User

複製代碼 代碼如下: protected void btnExcel_Click(object sender, EventArgs e)
{
List<bUFlow.Model.orderhistory> orderlist = dal.GetOrderList2("");
string filename = "3g流量網齡升級計劃使用者表";
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: #8ECBEA; height:25px; text-align:center; padding-left:10px;}</style>";
resp.Write(style);
//resp.Write(ExportTable(list));
resp.Write("<table class='table'><tr><th>" +"手機"+ "</th><th>" + "渠道" + "</th><th>" +"時間" + "</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();
}

需要注意的是編碼的問題,在輸出的時候,最好加上以下語句:複製代碼 代碼如下:<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.