asp.net導出Excel顯示中文亂碼的解決方法
protected void btnExcel_Click(object sender, EventArgs e)
{
List<bUFlow.Model.orderhistory> orderlist = dal.GetOrderList2("");
string filename = "3g流量網(wǎng)齡升級計劃用戶表";
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"/>
相關(guān)文章
.Net連接Oracle數(shù)據(jù)庫的實現(xiàn)代碼
本文使用System.Data.OracleClient方式,這是MS提供的以ADO.NET方式訪問Oracel的驅(qū)動;Oracle也有個Oracle Data Provider for .NET(ODP.NET)下篇再討論。2009-02-02.NET中應(yīng)用程序內(nèi)共享UdpClient聯(lián)機的實現(xiàn)方法
本篇文章介紹了,.NET中應(yīng)用程序內(nèi)共享UdpClient聯(lián)機的實現(xiàn)方法。需要的朋友參考下2013-05-05subsonic3.0插件更新字符串過長引發(fā)的異常修復方法
這篇文章主要介紹了subsonic3.0插件更新字符串過長引發(fā)的異常修復方法,需要的朋友可以參考下2014-04-04如何在ASP.NET Core 的任意類中注入Configuration
這篇文章主要介紹了如何在 ASP.NET Core 的任意類中注入Configuration ,幫助大家更好的理解和學習使用.net技術(shù),感興趣的朋友可以了解下2021-04-04ASP.NET實現(xiàn)多域名多網(wǎng)站共享Session值的方法
實現(xiàn)功能:可設(shè)置哪些站點可以共享Session值,這樣就防止別人利用這個去訪問,要想實現(xiàn)這個功能就必須得把Session值 放入數(shù)據(jù)庫中, 所有我們先在VS命令工具下注冊一個2011-11-11