示例#1
0
        /// <summary>
        /// 将CSV文件中的数据读取到DataTable
        /// </summary>
        /// <param name="file_path"></param>
        /// <returns></returns>
        public static DataTable OpenCSV(string file_path)
        {
            Encoding  encoding = EncodingTypeHelper.GetType(file_path);
            DataTable dt       = new DataTable();

            using (FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, encoding))
                {
                    string   strLine   = string.Empty;
                    string[] arryLine  = null;
                    string[] tableHead = null;
                    //标识列数
                    int columnCount = 0;
                    //标识是否读第一行
                    bool isFirst = true;
                    //逐行读取CSV中的数据
                    while ((strLine = sr.ReadLine()) != null)
                    {
                        if (isFirst)
                        {
                            tableHead   = strLine.Split(',');
                            isFirst     = false;
                            columnCount = tableHead.Length;
                            //创建列
                            for (int i = 0; i < columnCount; i++)
                            {
                                DataColumn column = new DataColumn(tableHead[i]);
                                dt.Columns.Add(column);
                            }
                        }
                        else
                        {
                            arryLine = strLine.Split(',');
                            DataRow dr = dt.NewRow();
                            for (int j = 0; j < columnCount; j++)
                            {
                                dr[j] = arryLine[j];
                            }
                            dt.Rows.Add(dr);
                        }
                    }

                    sr.Close();
                }
                fs.Close();
            }
            return(dt);
        }
示例#2
0
        public static Dictionary <int, string[]> ReadCSV(string file_path)
        {
            Encoding encoding = EncodingTypeHelper.GetType(file_path);
            Dictionary <int, string[]> dic = new Dictionary <int, string[]>();

            //int[] intArray = new int[11];

            using (FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, encoding))
                {
                    string   strLine   = string.Empty;
                    string[] arryLine  = null;
                    string[] tableHead = null;
                    //标识列数
                    int columnCount = 0;
                    //标识是否读第一行
                    bool isFirst = true;
                    //逐行读取CSV中的数据
                    while ((strLine = sr.ReadLine()) != null)
                    {
                        if (isFirst)
                        {
                            tableHead   = strLine.Split(',');
                            isFirst     = false;
                            columnCount = tableHead.Length;
                        }
                        else
                        {
                            arryLine = strLine.Split(',');
                            int key = Convert.ToInt32(arryLine[0]);
                            if (dic.ContainsKey(key))
                            {
                                continue;
                            }
                            dic.Add(key, arryLine);
                        }
                    }

                    sr.Close();
                }
                fs.Close();
            }
            return(dic);
        }