读CSV转换datatable

using System.Data;

using  System.IO;
 
/// <summary>
/// Stream读取.csv文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public  static  DataTable OpenCSV( string  filePath)
{
     DataTable dt =  new  DataTable();
     FileStream fs =  new  FileStream(filePath, FileMode.Open, FileAccess.Read);
     StreamReader sr =  new  StreamReader(fs, System.Text.Encoding.Default);
     //记录每次读取的一行记录
     string  strLine =  "" ;
     //记录每行记录中的各字段内容
     string [] aryLine;
     //标示列数
     int  columnCount = 0;
     //标示是否是读取的第一行
     bool  IsFirst =  true ;
     //逐行读取CSV中的数据
     while  ((strLine=sr.ReadLine())!= null )
     {
         aryLine = strLine.Split( ',' );
         if  (IsFirst== true )
         {
             IsFirst =  false ;
             columnCount = aryLine.Length;
             for  ( int  i = 0; i < columnCount; i++)
             {
                 DataColumn dc =  new  DataColumn(aryLine[i]);
                 dt.Columns.Add(dc);
             }
         }
         else
         {
             DataRow dr = dt.NewRow();
             for  ( int  j = 0; j < columnCount; j++)
             {
                 dr[j] = aryLine[j];
             }
             dt.Rows.Add(dr);
         }
     }
     sr.Close();
     fs.Close();
     return  dt;
}

猜你喜欢

转载自www.cnblogs.com/qiu18359243869/p/9625417.html