AI智能
改变未来

C# 实现TXT文档转Table

钉钉、微博极速扩容黑科技,点击观看阿里云弹性计算年度发布会!>>>

C# 实现TXT文档转

代码:

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 = \"\";//記錄每行記錄中的各字段内容string[] aryLine;//标示列数int columnCount = 0;//标示是否是读取的第一行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;//创建列for (int i = 0; i < columnCount; i++){DataColumn dc = new DataColumn(aryLine[i].ToUpper());dt.Columns.Add(dc);}}//逐行读取txt中的数據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;}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C# 实现TXT文档转Table