C# implementation of TXT document to table example code

code:

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);
      //Record the line read each time
      string strLine = "";
 
      //record the content of each field in each line of the record
      string[] aryLine;
      //Mark the number of columns      
      int columnCount = 0;
      //indicate whether it is the first line read
      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;
        //create raw
        for (int i = 0; i < columnCount; i++)
        {
          DataColumn dc = new DataColumn(aryLine[i].ToUpper());
          dt.Columns.Add(dc);
        }
      }
 
      //Read the data in txt line by line
      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;
    }
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);
      //Record the line read each time
      string strLine = "";

      //record the content of each field in each line of the record
      string[] aryLine;
      //Mark the number of columns      
      int columnCount = 0;
      //indicate whether it is the first line read
      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;
        //create column
        for (int i = 0; i < columnCount; i++)
        {
          DataColumn dc = new DataColumn(aryLine[i].ToUpper());
          dt.Columns.Add(dc);
        }
      }

      //record the content of each field in each line of the record
      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;
    }

The above is the C # implementation of TXT document to table example code, C # tutorial details, more about C # TXT document to table information please pay attention to

Read More: