c# 读取txt到datatable

/// <summary>

/// 读取Text文件,把数据存放到DataTable

/// </summary>

/// <param name="dt">存放数据的DataTable</param>

/// <param name="TextFile">Text文件名</param>

public static void ReadTextFile(ref DataTable dt, string TextFile)

{

dt.Rows.Clear();

if (System.IO.File.Exists(TextFile))

{

System.IO.FileStream fs = new System.IO.FileStream(TextFile, System.IO.FileMode.Open);

System.IO.StreamReader m_streamReader = new System.IO.StreamReader(fs, System.Text.Encoding.Default);

//m_streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);

string strLine = m_streamReader.ReadLine();

//读取文件内容

while (!string.IsNullOrEmpty(strLine))

{

DataRow drNew = dt.NewRow();

string\[\] arrItem = strLine.Split(new char\[\] { splitStr }, StringSplitOptions.None);

for (int i = 0; i < arrItem.Length; i++)

{

if (i < dt.Columns.Count)

{

drNewi = arrItemi;

}

else

{

break;

}

}

dt.Rows.Add(drNew);

strLine = m_streamReader.ReadLine();

}

m_streamReader.Close();

}

}

相关推荐
码途行者2 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
头发还在的女程序员3 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同
java·开发语言·充电桩系统·充电桩管理·充电系统·充电小程序
timeDifference₇4 小时前
Python Vibe Coding实操实验报告
开发语言·jvm·python
geovindu5 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar5 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
rockey6276 小时前
AScript正式推出Template模板引擎
c#
geovindu6 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
IT空门:门主7 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线
java·学习
沐言Agent8 小时前
做工业上位机,这个 C# 开源框架把 PLC 通讯、流程控制和 MES 对接都备好了
c#·工业上位机
用户3721574261358 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java