国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 導(dǎo)出到CSV文件亂碼的問題

2024-07-10 13:26:07
字體:
供稿:網(wǎng)友

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();

重點為紅色標(biāo)記處!

------

string strFile = "FileName" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv";

//這一部分替換為你從DataTable/GridView獲取的內(nèi)容
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();

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 周宁县| 慈利县| 长顺县| 永丰县| 新和县| 安新县| 曲周县| 女性| 旬邑县| 佳木斯市| 正定县| 云和县| 东方市| 固原市| 高阳县| 哈巴河县| 民和| 淄博市| 兴安县| 白朗县| 普兰县| 九江市| 金山区| 白朗县| 普洱| 淄博市| 马山县| 时尚| 贺州市| 麻栗坡县| 得荣县| 正镶白旗| 兴和县| 张家界市| 烟台市| 阿拉善左旗| 尤溪县| 嫩江县| 晋州市| 信阳市| 平潭县|