C#导出DataTable数据到CSV文件中
2015-11-26 13:27
507 查看
public void WriteData(DataTable dt, string path) { try { System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write); StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GBK")); for (int i = 0; i < dt.Columns.Count; i++) { sw.Write(dt.Columns[i].ColumnName); if (i != dt.Columns.Count - 1) { sw.Write(","); } } sw.WriteLine(""); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { sw.Write(DelSpecialChar(dt.Rows[i][j].ToString())); if (j != dt.Columns.Count - 1) { sw.Write(",");//分隔符 } } sw.WriteLine(""); } sw.Flush(); sw.Close(); } catch {} } public string DelSpecialChar(string str) { string result = str; string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "`", ";", "'", ",", ".", "/", "/,", "<", ">", "?" }; for (int i = 0; i < strQuota.Length; i++) { if (result.IndexOf(strQuota[i]) > -1) result = result.Replace(strQuota[i], ""); } return result; }
相关文章推荐
- C#中hashtable的赋值、取值、遍历、排序操作
- csharp:百度翻译
- csharp:百度翻译
- csharp:百度翻译
- C#导出数据到Excel通用的方法类
- C# repeater控件序号绑定
- C#文件夹路径获得
- C#中虚方法,抽象类(方法)和接口的区别
- C# 属性和字段的区别
- 【术】深入理解c#反射
- (转)C# Enum,Int,String的互相转换 枚举转换
- [转]C#取得内网IP、外网IP、客户端IP方法
- Windows Server 2008 部署webservice(C# .Net)
- 【C#】Event事件的应用之观察者模式
- 【C#】匿名方法的终结者——Lambda表达式
- 【术】c#中利用反射实现深拷贝
- C#中通过Selenium IWebDriver实现人人网相册备份工具
- C# FTP 上传 下载(汇总)
- C# FTP 上传 下载(汇总)
- WPF中C#代码触发鼠标点击事件