SoFunction
Updated on 2025-03-07

C# Example code for implementing TXT document to Table

C# Example code for implementing TXT document to Table

Updated: December 9, 2020 08:47:55 Author: Yuanzi Life
This article mainly introduces the sample code of C# to implement TXT document to Table, which helps everyone better understand and learn C#. Interested friends can learn about it.

Code:

public DataTable TXTToDataTable(string fileName, string columnName)
    {
      DataTable dt = new DataTable();
      FileStream fs = new FileStream(fileName, , );
      StreamReader sr = new StreamReader(fs, );
      //Record a line of records for each read      string strLine = "";

      //Record the contents of each field in the record of each line      string[] aryLine;
      //Display the number of columns      int columnCount = 0;
      //Sign whether it is the first line of read      bool IsFirst = true;

      if (IsFirst == true)
      {
        //strLine = "ATTENDANCE_DATE,EMP,ATTENDANCE_DEPT,EMP_TYPE,SHITF,PLANT_CODE";
        strLine = columnName;
        aryLine = (',');
        IsFirst = false;
        columnCount = ;
        //Create a column        for (int i = 0; i < columnCount; i++)
        {
          DataColumn dc = new DataColumn(aryLine[i].ToUpper());
          (dc);
        }
      }

      //Read the data in txt line by line      while ((strLine = ()) != null)
      {
        aryLine = ('\t');//tab separator        DataRow dr = ();
        for (int j = 0; j < columnCount; j++)
        {
          dr[j] = aryLine[j].ToUpper();
        }
        (dr);
      }

      ();
      ();
      return dt;
    }

The above is the detailed content of the sample code for C# to implement TXT document to Table. For more information about c# TXT document to Table, please pay attention to my other related articles!

  • c#
  • txt
  • document
  • table

Related Articles

  • C# Winform implementation example of exporting DataGridView to Excel

    This article mainly introduces the implementation example of DataGridView exported to Excel in C# Winform. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.
    2022-05-05
  • Detailed explanation of partitions in c# PLINQ

    This article mainly introduces the relevant information about partitions in c# PLINQ. The sample code in the article is very detailed to help everyone better understand and learn. Interested friends can learn about it.
    2020-07-07
  • How to read and write application configuration files in C# and display them on the interface

    This article mainly introduces how C# reads and writes application configuration files and displays problems on the interface. It has good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2023-06-06
  • Comparative explanation of C# single-threaded and multi-threaded port scanner applications

    This article mainly compares and analyzes the C# single-threaded and multi-threaded port scanner applications in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2022-07-07
  • c# winform multi-threaded dead loop treads the pit

    This article mainly introduces the c# winform multi-threaded dead loop trap. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.
    2023-12-12
  • Three ways to send emails in C#

    This article mainly introduces three methods of C# to implement sending emails. The examples describe three methods of Localhost, SMTP and SSL-SMTP. They have good reference value for C# project development. Friends who need it can refer to it.
    2014-11-11
  • C# Detailed example of using windows service to update site map

    This article mainly introduces a detailed example of using Windows service to update site maps. Friends who need it can refer to it.
    2014-04-04
  • Silverlight file upload and download implementation method (download and save)

    This article mainly introduces the method of uploading and downloading of Silverlight files (download and save). Friends who need it can refer to it.
    2015-11-11
  • C# Implementing the Tower of Hannover

    This article mainly introduces examples of C# implementing the Tower of Hannover game, helping everyone better understand and use the C# programming language. Interested friends can learn about it.
    2020-11-11
  • Unity's AssetPostprocessor Model animation function usage case study

    This article mainly introduces an in-depth analysis of Unity's AssetPostprocessor Model animation function usage cases. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase as soon as possible.
    2023-08-08

Latest Comments