C# 實現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼
更新時間:2020年12月09日 08:47:55 作者:農(nóng)碼一生
這篇文章主要介紹了C# 實現(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# 實現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于c# TXT文檔轉(zhuǎn)Table的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
相關(guān)文章
C#利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了C#潤滑利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動手嘗試一下2022-07-07

