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)

{

drNew[i] = arrItem[i];

}

else

{

break;

}

}

dt.Rows.Add(drNew);

strLine = m_streamReader.ReadLine();

}

m_streamReader.Close();

}

}

相关推荐
脸大是真的好~4 分钟前
计算机408基础相关面试题-备用,不推荐
java
云上漫步者5 分钟前
深度实战:Rust交叉编译适配OpenHarmony PC——unicode_width完整适配案例
开发语言·后端·rust·harmonyos
小费的部落5 分钟前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
咘噜biu6 分钟前
多租户动态数据源插件dynamic-datasource简介
java·mybatisplus·动态数据源·多租户
漫漫求8 分钟前
Java内存模型【JMM】、JVM内存模型
java·开发语言·jvm
原来是好奇心8 分钟前
深入Spring Boot源码(五):外部化配置与Profile机制深度解析
java·源码·springboot
IT界的奇葩9 分钟前
OAuth2 单点登录流程图
java·流程图·oauth2·单点登录·sso
田姐姐tmner14 分钟前
Python 全面语法指南
开发语言·python
white-persist23 分钟前
【攻防世界】reverse | simple-check-100 详细题解 WP
c语言·开发语言·汇编·数据结构·c++·python·算法
wuguan_27 分钟前
C#中的静态成员、常量和只读变量
开发语言·c#