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

}

}

相关推荐
Gerardisite1 分钟前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
Want5954 分钟前
C/C++跳动的爱心①
c语言·开发语言·c++
coderxiaohan33 分钟前
【C++】多态
开发语言·c++
gfdhy43 分钟前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
闲人编程1 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
Eiceblue1 小时前
通过 C# 将 HTML 转换为 RTF 富文本格式
开发语言·c#·html
故渊ZY1 小时前
Java 代理模式:从原理到实战的全方位解析
java·开发语言·架构
匿者 衍1 小时前
POI读取 excel 嵌入式图片(支持wps 和 office)
java·excel
leon_zeng01 小时前
Qt Modern OpenGL 入门:从零开始绘制彩色图形
开发语言·qt·opengl
会飞的胖达喵1 小时前
Qt CMake 项目构建配置详解
开发语言·qt