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();

}

}

相关推荐
特立独行的猫A1 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
一起_java1 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
Flynn Liang3 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
吃我一窝jio4 小时前
Python与Go线程模型深度对比
java·开发语言·python
这个昵称不容易4 小时前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图
java·springboot·毕设
文丑颜不良啊4 小时前
LeetCode3041~3060题目解析
java·leetcode
eggwyw4 小时前
C++ union 的一种妙用法
开发语言·c++
文丑颜不良啊6 小时前
LeetCode3101~3120题目解析
java·leetcode
Resean02236 小时前
SpringMVC 6+源码分析(十一)视图渲染与异步请求(收尾篇)
java·spring·springmvc
何以解忧唯有撸码6 小时前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi