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

}

}

相关推荐
程序 代码狂人几秒前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
2501_944521591 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
meiying88381 小时前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化
@大迁世界2 小时前
告别 Go,拥抱 Rust
开发语言·golang·rust
SEO_juper2 小时前
Node.js 重定向链检测工具:301 跳了几跳、有没有成环,一次看清
xml·开发语言·python·node.js·json·bash
南归北隐2 小时前
Spring AI Alibaba Graph框架实现MCP数据库查询实战
java·后端·spring ai·mcp·spring ai mcp
学逆向的2 小时前
C 指针常量与常量指针
c语言·开发语言·网络安全
古法老登瞎折腾2 小时前
大数据智能运维实战(九):让 AI 查日志——找到异常,也保留上下文
java·大数据·运维·人工智能·spring ai·mcp
ai大模型-探索者2 小时前
Function Call与MCP(区别、联系与实战)
java·服务器·网络
略略略咯咯3 小时前
啊啊啊啊啊
java·服务器·数据库