asp.net導(dǎo)出Excel顯示中文亂碼的解決方法
protected void btnExcel_Click(object sender, EventArgs e)
{
List<bUFlow.Model.orderhistory> orderlist = dal.GetOrderList2("");
string filename = "3g流量網(wǎng)齡升級(jí)計(jì)劃用戶(hù)表";
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>" +"手機(jī)"+ "</th><th>" + "渠道" + "</th><th>" +"時(shí)間" + "</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();
}
需要注意的是編碼的問(wèn)題,在輸出的時(shí)候,最好加上以下語(yǔ)句:
<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>
相關(guān)文章
.Net連接Oracle數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
本文使用System.Data.OracleClient方式,這是MS提供的以ADO.NET方式訪問(wèn)Oracel的驅(qū)動(dòng);Oracle也有個(gè)Oracle Data Provider for .NET(ODP.NET)下篇再討論。2009-02-02ASP.NET中后臺(tái)注冊(cè)js腳本使用的方法對(duì)比
接下來(lái)為大家介紹下使用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區(qū)別2013-04-04.NET中應(yīng)用程序內(nèi)共享UdpClient聯(lián)機(jī)的實(shí)現(xiàn)方法
本篇文章介紹了,.NET中應(yīng)用程序內(nèi)共享UdpClient聯(lián)機(jī)的實(shí)現(xiàn)方法。需要的朋友參考下2013-05-05subsonic3.0插件更新字符串過(guò)長(zhǎng)引發(fā)的異常修復(fù)方法
這篇文章主要介紹了subsonic3.0插件更新字符串過(guò)長(zhǎng)引發(fā)的異常修復(fù)方法,需要的朋友可以參考下2014-04-04ASP.NET?MVC使用Identity增刪改查用戶(hù)
這篇文章介紹了ASP.NET?MVC使用Identity增刪改查用戶(hù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10如何在ASP.NET Core 的任意類(lèi)中注入Configuration
這篇文章主要介紹了如何在 ASP.NET Core 的任意類(lèi)中注入Configuration ,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04ASP.NET實(shí)現(xiàn)多域名多網(wǎng)站共享Session值的方法
實(shí)現(xiàn)功能:可設(shè)置哪些站點(diǎn)可以共享Session值,這樣就防止別人利用這個(gè)去訪問(wèn),要想實(shí)現(xiàn)這個(gè)功能就必須得把Session值 放入數(shù)據(jù)庫(kù)中, 所有我們先在VS命令工具下注冊(cè)一個(gè)2011-11-11