csv操作帮助类
2015-11-26 11:37
471 查看
功能描述:
集合转换为csv数据
DataSe转换为csv数据
集合转换为csv数据
DataSe转换为csv数据
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace IM.Common { /// <summary> /// csv操作 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/06/17</para> /// </summary> public class CsvHelper { public static string _seperator = ","; public static string _bracer = "\""; public static string _newline = "\n"; public static string _regex_special = ".*[,\n\"].*"; /// <summary> /// 集合转换为csv数据 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/06/17</para> /// </summary> /// <param name="list">数据集合,行、列</param> /// <returns>csv数据</returns> public static string ToCsvData(List<List<string>> list) { string temp = string.Empty; System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (List<string> row in list) { for (int i = 0; i < row.Count; i++) { temp = row[i]; temp = (temp != null ? temp : ""); if (i != 0) { sb.Append(_seperator); } if (temp.Contains(_seperator) || temp.Contains(_bracer) || temp.Contains(_newline)) { if (temp.Contains(_bracer)) { temp = temp.Replace(_bracer, _bracer + _bracer); } if (temp.Contains(_newline)) { temp = temp.Replace(_newline, "<br/>"); } sb.Append(_bracer + temp + _bracer); } else { sb.Append(temp); } if (i == row.Count - 1) { sb.Append(_newline); } } } return sb.ToString(); } /// <summary> /// DataSe转换为csv数据 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/06/17</para> /// </summary> /// <param name="ds">数据集</param> /// <returns>csv数据</returns> public static string ToCsvData(DataSet ds) { List<List<string>> list = new List<List<string>>(); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null) { DataTable dt = ds.Tables[0]; List<string> columnNameList = new List<string>(); //添加列 if (dt.Columns.Count > 0) { foreach (DataColumn column in dt.Columns) { columnNameList.Add(column.ColumnName); } list.Add(columnNameList); } //迭代添加行记录 foreach (DataRow row in dt.Rows) { List<string> rowData = new List<string>(); foreach (string columnName in columnNameList) { rowData.Add(Convert.ToString(row[columnName])); } list.Add(rowData); } } return ToCsvData(list); } } }
相关文章推荐
- java代码检查工具-FindBugs
- 如果要创业,你应该如何选择创业方向?
- Memcached安装配置
- svn服务搭建
- 自定义UINavigationController 切换动画
- Bootstrap每天必学之按钮
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- app评论
- UltraEdit相关设置
- HTML与JSP页面的区别
- pymol png python 不输出图片 ray解决方法 ,
- opengl绘制出现锯齿的一个可能原因
- C#导出数据到Excel通用的方法类
- ehcache页面缓存
- 【Linux】vim常用快捷键
- json解析
- CocoaPods环境配置、安装和使用教程
- 操作系统和编译器如何识别全局变量和局部变量