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)文章!
您可能感興趣的文章:
- C# DataTable與Model互轉(zhuǎn)的示例代碼
- c# 遍歷獲取所有文件的示例代碼
- C#彈出對(duì)話框確定或者取消執(zhí)行相應(yīng)操作的實(shí)例代碼
- C# 進(jìn)行圖片壓縮的示例代碼(對(duì)jpg壓縮效果最好)
- ToLua框架下C#與Lua代碼的互調(diào)操作
- c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn)
- C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼
- C# 利用Selenium實(shí)現(xiàn)瀏覽器自動(dòng)化操作的示例代碼
- C# 實(shí)現(xiàn)dataGridView選中一行右鍵出現(xiàn)菜單的示例代碼
- c# 如何實(shí)現(xiàn)代碼生成器
相關(guān)文章
C#運(yùn)算符大全_各種運(yùn)算符號(hào)的概述及作用
以下是對(duì)C#中各種運(yùn)算符號(hào)的說明及作用進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10
C#利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了C#潤滑利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07

