将文件都入到DataTable中,及datatable 转换为某对象

private DataTable GetDataTableByFile(string path)
        {
            DataTable dt = new DataTable();
            FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            StreamReader sr = new StreamReader(fs);
            string strLine = "";
            string[] aryLine = null;
            string[] tableHead = null;
            int columnCount = 0;
            bool IsFirst = true;
            while ((strLine = sr.ReadLine()) != null)
            {
                if (IsFirst == true)
                {
                    tableHead = strLine.Split(',');
                    IsFirst = false;
                    columnCount = tableHead.Length;
                    for (int i = 0; i < columnCount; i++)
                    {
                        DataColumn dc = new DataColumn(tableHead[i]);
                        dt.Columns.Add(dc);
                    }
                }
                else
                {
                    aryLine = strLine.Split(',');
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < columnCount; j++)
                    {
                        dr[j] = aryLine[j];
                    }
                    dt.Rows.Add(dr);
                }
            }
            if (aryLine != null && aryLine.Length > 0)
            {
                dt.DefaultView.Sort = tableHead[0] + " " + "asc";
            }

            sr.Close();
            fs.Close();

            return dt;
        }

public List<ClassA> GetSource(string path)
        {
            List<ClassA> listClass= new List<ClassA>();
            AutoLabelEntity autoLabelEntity = null;
            foreach (DataRow item in GetDataTableByFile(path).Rows)
            {
                ClassA= new ClassA()
                {
                    aa= item["Source"].ToString(),
                    bb= item["SubmissionID"].ToString(),
                    cc= item["Sha1"].ToString(),
                    dd= item["AutoLabel"].ToString()
                };

                listClass.Add(ClassA);
            }

            return listClass;
        }

posted on 2016-04-21 16:58  昂扬绽放  阅读(136)  评论(0编辑  收藏  举报

导航