asp.net 導出到CSV文件亂碼的問題
http://social.microsoft.com/Forums/zh-CN/295/thread/14a833f5-95bf-48ef-b6cf-c6028f338561
string name = System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString();
FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine(" 自動編號,姓名,年齡");
foreach (DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"] + "," + dr["vName"] + "," + dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";// 指定返回的是一個不能被客戶端讀取的流,必須被下載
Response.WriteFile(name); // 把文件流發(fā)送到客戶端
Response.End();
重點為紅色標記處!
------
string strFile = "FileName" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv";
//這一部分替換為你從DataTable/GridView獲取的內容
StringBuilder sb = new StringBuilder();
sb.AppendLine("id,name");
sb.AppendLine("1,鄒俊才");
sb.AppendLine("2,才俊鄒");
sb.AppendLine("3,Jon");
StringWriter sw = new StringWriter(sb);
sw.Close();
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", strFile));
Response.Charset = "gb2312";
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Write(sw);
Response.Flush();
Response.End();
相關文章
AspNetPager分頁控件UrlRewritePattern參數(shù)設置的重寫代碼
AspNetPager分頁控件UrlRewritePattern參數(shù)設置的重寫代碼,需要的朋友可以參考一下2013-02-02.Net中如何操作IIS的虛擬目錄原理分析及實現(xiàn)方案
編程控制IIS實際上很簡單,和ASP一樣,.Net中需要使用ADSI來操作IIS,但是此時我們不再需要GetObject這個東東了,因為Net為我們提供了更加強大功能的新東東2012-12-12