CSV文件解释代码

来源:互联网 发布:长春盘古网络咋样 编辑:程序博客网 时间:2024/06/10 19:58

CSV文档规则
1:数据使用逗号分隔;
2:内容中可以包括用于分隔数据的逗号,此类数据使用双引号括起;
3:数据中如果包括双引号,需要使用两个双引号;

代码中没有使用Remove和Substring等函数对源串进行处理,而是使用了索引来定位字符串值。

下面的代码将CSV文件解释出的数据存放到DataTable中以便使用。附计算的活动图。

 

        /// <summary>        /// csv文件解释        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnCallcsv_Click(object sender, EventArgs e)        {            FileStream fileStream = new FileStream(@"D:\temp\a.csv", FileMode.Open, FileAccess.Read, FileShare.None);            //标识读取的是否是第一行,第一行是列名            bool columnFlag = true;            //定义新数据行            string strLine = null;            DataTable table = new DataTable();            using (StreamReader sr = new StreamReader(fileStream, System.Text.Encoding.GetEncoding("gb2312")))            {                while (!mysr.EndOfStream)                {                    strLine = mysr.ReadLine();                    if (!string.IsNullOrEmpty(strLine))                    {                        //lihx 2013年4月7日 实际上这里会有问题,因为CSV中,使用“”括起来的内容包括“,”的时候,逗号不被解释为分隔符,比如:“"2-邮局汇款,"”                        //读取第一行,作为列名                        if (columnFlag)                        {                            columnFlag = false;                            int lineStartIndex = 0;                            while (lineStartIndex < strLine.Length)                            {                                table.Columns.Add(getSPValue(sr, ref lineStartIndex, ref strLine));                            }                        }                        else                        {                            //初始化新行,并放入table中                            int itemIndex = 0;                            int lineStartIndex = 0;                            object[] items = new object[table.Columns.Count];                            while (lineStartIndex < strLine.Length && itemIndex < items.Length)                            {                                items[itemIndex++] = getSPValue(sr, ref lineStartIndex, ref strLine);                            }                            table.Rows.Add(items);                        }                    }                }            }        }}
private static string getSPValue(StreamReader sr, ref int lineStartIndex, ref string strLine){int spIndex = lineStartIndex;if (lineStartIndex >= strLine.Length){return string.Empty;}if (strLine[spIndex] == '\"'){lineStartIndex++;int tmpIndex = -1;while ((tmpIndex = strLine.IndexOf("\"", spIndex + 1)) == -1){string nextLine = sr.ReadLine();strLine += "\n" + nextLine;}spIndex = tmpIndex;if (spIndex == -1){spIndex = strLine.Length;}}spIndex = strLine.IndexOf(",", spIndex);if (spIndex == -1){spIndex = strLine.Length;}if (lineStartIndex > -1 && lineStartIndex < strLine.Length){string tmpValue = strLine.Substring(lineStartIndex, spIndex - lineStartIndex);if (tmpValue != null && tmpValue.Length > 1 && tmpValue.StartsWith("\"") && tmpValue.EndsWith("\"")){tmpValue = tmpValue.Substring(1, tmpValue.Length - 2);if (tmpValue.Length > 0){tmpValue = tmpValue.Replace("\"\"", "\"");}}//考虑到性能不建议使用以下截串的算法。//strline = strline.Substring(spIndex + 1);lineStartIndex = spIndex + 1;return tmpValue;}lineStartIndex = spIndex + 1;return string.Empty;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 辞职后奖金不发怎么办 小孩被水母蛰了怎么办 被剧毒水母蛰了怎么办 被水母蛰了很痒怎么办 ipo被否了将来怎么办 科技布沙发裂了怎么办 35岁皮肤皱纹多怎么办 22岁皮肤皱纹多怎么办 腿上皮肤皱纹多怎么办 16岁皮肤有皱纹怎么办 28岁皮肤有皱纹怎么办 眼睛上有血管翳怎么办 鸽子家飞时间短怎么办 宝宝吃母乳不吃奶瓶怎么办 邮件群发超50人怎么办 鸽子拉白色水便怎么办 进京没办进京证怎么办 吃海鲜喝牛奶了怎么办 煎牛排油少了怎么办 网上订酒店去后怎么办 澳洲语言班没过怎么办 照片粘在玻璃上怎么办 照片粘玻璃上怎么办啊 个人3月旅游签证怎么办 澳洲签证拒签了怎么办 我想买房子可是没钱怎么办 微信身份证17位怎么办 身份证后4位泄露怎么办 车牌照掉了一个怎么办 车牌螺丝孔坏了怎么办 借了贷款还不起怎么办 谷歌验证码丢了怎么办 谷歌验证码错误怎么办 未满16岁怎么办银行卡 身份证在北京丢了怎么办 出国前护照丢了怎么办 在美国护照丢了怎么办 护照上照片太丑怎么办 小额贷还不起了怎么办 护照不到6个月怎么办 出国的护照是怎么办的