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:
- You might want to save ‘/run/in itramf s/rdsosreport.txt“ to a USB stick or bootaf ter mounting
- [How to Fix]Element is not attached to the page document record
- [Solved] Linux — 9 — txt files are copied from windows to Linux and read error
- Ubuntu: pdserving deployment output log.txt Error
- [Solved] Linux C++ warning: ISO C++ forbids converting a string constant to ‘char*‘ [-Wwrite-strings]
- Sublime text 3 compiles and executes C/C++ programs directly
- [Solved] catkin_make error: CMake Error at CMakeLists.txt:1:
- C#: How to Use Itextsharp to Manipulate PDF Files
- Linux Error: CMake Error in CMakeLists.txt: No CMAKE_CXX_COMPILER could be found.
- [Solved] Ubuntu Eclipse C/C++ Error: launch failed.binary not found
- How to Solve C++ Error: “no matching function for call to ‘atoi/atof’”
- MAC: Clion configure C compiler Error: The C compiler identification is unknown
- [Solved] check_urdf demo05_test.urdf Error: Error document empty. at line 71 in /build/urdfdom-YM
- hb set Error: OHOS ERROR] Invalid vendor path: /home/openharmony/vendor (openharmony Compile ubuntu20.04 official document)
- Disk Extended Error: WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
- C# WPF Framework: How to Build Caliburn.Micro Quickly
- C#: How to get the value or text value of the select drop-down list
- Vector series in actual C + +_ To_ fit()
- [Solved] Execute ./configure Error: [error]configure: error: You need a C++ compiler for C++ support.
- C Language Compilation Error: variably modified ‘* *’ at file scope