如何将 txt 导入 datatable

tyhuang 2009-05-11 05:05:31
txt 格式如下
11111
22222
3333 333
4444 44444
55555
没有分割符
...全文
297 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-05-11
  • 打赏
  • 举报
回复
public DataTable DTtxt(string filePath,string fileName)
{
FileStream fs = new FileStream(filePath + "\\" + fileName,FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr=new StreamReader(fs,Encoding.GetEncoding("GB2312"));
string StrLine=sr.ReadToEnd();
sr.Close();

string[] ArryList=Regex.Split(StrLine,"\n");

DataTable DT=new DataTable();
DT.Columns.Add("a");
string[] arrylist={};
if(ArryList.Length>0)
{
for(int i=0;i<ArryList.Length;i++)
{
arrylist=ArryList[i].ToString().Split('|');
DT.Rows.Add(arrylist);
}
DT.AcceptChanges();
}
return DT;
}

十八道胡同 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxg22526451 的回复:]


C# code //读取所有行
string[] sarr = File.ReadAllLines(@"c:\123.txt");
DataTable dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
foreach (string s in sarr)
{
//添加到DataTable
dt.Rows.Add(s);

}
[/Quote]


up up
wxg22526451 2009-05-11
  • 打赏
  • 举报
回复
一列就好办了。
            //读取所有行
string[] sarr = File.ReadAllLines(@"c:\123.txt");
DataTable dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
foreach (string s in sarr)
{
//添加到DataTable
dt.Rows.Add(s);

}
tyhuang 2009-05-11
  • 打赏
  • 举报
回复
插入一列
wxg22526451 2009-05-11
  • 打赏
  • 举报
回复
没有分割符?怎么区分列?
wxg22526451 2009-05-11
  • 打赏
  • 举报
回复
            //读取所有行
string[] sarr = File.ReadAllLines(@"c:\123.txt");
foreach (string s in sarr)
{
//添加到DataTable
}
jiangzhe556 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qb81195235 的回复:]
C# code
System.IO.TextReader tr = System.IO.File.OpenText("d:\\test123.txt");
System.Collections.ArrayList al = new ArrayList();

string tmp = tr.ReadLine();
while(tmp != null)
{
al.Add(tmp);
tmp = tr.ReadLine();
}

tr.Close();

string[] txts = new string[al.Count];
al.CopyTo(txts,0);

[/Quote]
ddd
yanlan112 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qb81195235 的回复:]
C# code
System.IO.TextReader tr = System.IO.File.OpenText("d:\\test123.txt");
System.Collections.ArrayList al = new ArrayList();

string tmp = tr.ReadLine();
while(tmp != null)
{
al.Add(tmp);
tmp = tr.ReadLine();
}

tr.Close();

string[] txts = new string[al.Count];
al.CopyTo(txts,0);

[/Quote]
qb81195235 2009-05-11
  • 打赏
  • 举报
回复

System.IO.TextReader tr = System.IO.File.OpenText("d:\\test123.txt");
System.Collections.ArrayList al = new ArrayList();

string tmp = tr.ReadLine();
while(tmp != null)
{
al.Add(tmp);
tmp = tr.ReadLine();
}

tr.Close();

string[] txts = new string[al.Count];
al.CopyTo(txts,0);

//读取TXT文件每行的数据

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧