欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 實(shí)現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼

 更新時(shí)間:2020年12月09日 08:47:55   作者:農(nóng)碼一生  
這篇文章主要介紹了C# 實(shí)現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

代碼:

public DataTable TXTToDataTable(string fileName, string columnName)
    {
      DataTable dt = new DataTable();
      FileStream fs = new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
      StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
      //記錄每次讀取的一行記錄
      string strLine = "";

      //記錄每行記錄中的各字段內(nèi)容
      string[] aryLine;
      //標(biāo)示列數(shù)      
      int columnCount = 0;
      //標(biāo)示是否是讀取的第一行
      bool IsFirst = true;

      if (IsFirst == true)
      {
        //strLine = "ATTENDANCE_DATE,EMP,ATTENDANCE_DEPT,EMP_TYPE,SHITF,PLANT_CODE";
        strLine = columnName;
        aryLine = strLine.Split(',');
        IsFirst = false;
        columnCount = aryLine.Length;
        //創(chuàng)建列
        for (int i = 0; i < columnCount; i++)
        {
          DataColumn dc = new DataColumn(aryLine[i].ToUpper());
          dt.Columns.Add(dc);
        }
      }

      //逐行讀取txt中的數(shù)據(jù)
      while ((strLine = sr.ReadLine()) != null)
      {
        aryLine = strLine.Split('\t');//tab分隔符
        DataRow dr = dt.NewRow();
        for (int j = 0; j < columnCount; j++)
        {
          dr[j] = aryLine[j].ToUpper();
        }
        dt.Rows.Add(dr);
      }

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

以上就是C# 實(shí)現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于c# TXT文檔轉(zhuǎn)Table的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論