首页 > 其他 > 详细

按格式读取csv文件内容

时间:2014-07-12 14:54:28      阅读:374      评论:0      收藏:0      [点我收藏+]
    string path = @"C:\Users\keen_\Downloads\upload\upload\Upload\20140701141934_export.csv";
    ImportDataTable(path);

        //2014-07-01
        //get csv file to datatable
        private static DataTable ImportDataTable(string filepath)
        {
            DataTable mydt = new DataTable("myTableName");
            mydt.Columns.Add("Data ID", System.Type.GetType("System.String"));
            mydt.Columns.Add("Field Name", System.Type.GetType("System.String"));
            mydt.Columns.Add("New Value", System.Type.GetType("System.String"));
            DataRow mydr;
            using (System.IO.StreamReader mysr = new System.IO.StreamReader(filepath))
            {
                int data;
                char current;
                StringBuilder text = new StringBuilder();

                IDictionary<int, List<string>> results = new Dictionary<int, List<string>>();
                bool isInYinHao = false; ;
                int lineId = 1;
                int index = 0;
                while (true)
                {
                    data = mysr.Read();
                    if (data != -1)
                    {
                        current = (char)data;
                        if (current == ")
                        {
                            if (isInYinHao)
                            {
                                isInYinHao = false;
                            }
                            else
                            {
                                if (index > 0)
                                {
                                    text.Append(current);
                                }

                                isInYinHao = true;
                            }
                        }
                        else if (current == ,)
                        {
                            if (isInYinHao)
                            {
                                text.Append(current);
                            }
                            else
                            {

                                SaveResult(results, lineId, text);
                                index = 0;
                                continue;
                            }
                        }
                        else if (current == \r)
                        {
                            if (isInYinHao)
                            {
                                text.Append(current);
                            }
                        }
                        else if (current == \n)
                        {
                            if (isInYinHao)
                            {
                                text.Append(current);
                            }
                            else
                            {
                                SaveResult(results, lineId, text);
                                index = 0;
                                lineId++;
                                continue;
                            }
                        }
                        else if (current == \0)
                        {
                        }
                        else
                        {
                            text.Append(current);
                        }

                        index++;
                    }
                    else
                    {
                        //Read to file end.
                        SaveResult(results, lineId, text);
                        break;
                    }
                }

                foreach (int id in results.Keys)
                {
                    mydr = mydt.NewRow();
                    for (int i = 0; i < results[id].Count; i++)
                    {
                        if (i > 2)
                        {
                            break;
                        }

                        mydr[i] = results[id][i];
                    }

                    mydt.Rows.Add(mydr);
                }

            }

            return mydt;
        }
        private static void SaveResult(IDictionary<int, List<string>> results, int lineId, StringBuilder text)
        {
            if (!results.ContainsKey(lineId))
            {
                results.Add(lineId, new List<string>());
            }

            results[lineId].Add(text.ToString());
            text.Remove(0, text.Length);
        }

 

按格式读取csv文件内容,布布扣,bubuko.com

按格式读取csv文件内容

原文:http://www.cnblogs.com/kennyliu/p/3839911.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!