C#怎样把得到的txt文件数据导入DataTable里面!
读取txt文件里面的数据,然后像操作数据库表那样,根据ID查找,例如txt文件里面数据时这样的,ABCD1,34,ff,562,34,dd,783,20,tr,77ABC...
读取txt文件里面的数据,然后像操作数据库表那样,根据ID查找,
例如txt文件里面数据时这样的,
A B C D
1,34,ff,56
2,34,dd,78
3,20,tr,77
ABCD是假设给一个字段!
我的txt里面有很多条数据,我只举出3行数据!
我用下面的那个方法得到txt数据,
想导入到DataTable 再实现根据查找B=34得到所有数据!
请高手指点一下,最好给点代码,谢谢!
List<string> liststr = SystemShareManager.getFile();
foreach (string path in liststr)
{
string conten;
int currentLine = 0;
//判断txt文件是否存在
if (File.Exists(path) == true)
{
StreamReader sysr = new StreamReader(path, UnicodeEncoding.GetEncoding("GBK"));
StringBuilder sb = new StringBuilder();
//读取txt文件,不读第一行数据
while (sysr.Peek() > -1)
{
if (currentLine == 0)
{
sysr.ReadLine();
}
else
{
sb.AppendLine(sysr.ReadLine());
}
currentLine++;
}
conten = sb.ToString();
this.txtContene.Text += conten;
sysr.Close();
}
continue; 展开
例如txt文件里面数据时这样的,
A B C D
1,34,ff,56
2,34,dd,78
3,20,tr,77
ABCD是假设给一个字段!
我的txt里面有很多条数据,我只举出3行数据!
我用下面的那个方法得到txt数据,
想导入到DataTable 再实现根据查找B=34得到所有数据!
请高手指点一下,最好给点代码,谢谢!
List<string> liststr = SystemShareManager.getFile();
foreach (string path in liststr)
{
string conten;
int currentLine = 0;
//判断txt文件是否存在
if (File.Exists(path) == true)
{
StreamReader sysr = new StreamReader(path, UnicodeEncoding.GetEncoding("GBK"));
StringBuilder sb = new StringBuilder();
//读取txt文件,不读第一行数据
while (sysr.Peek() > -1)
{
if (currentLine == 0)
{
sysr.ReadLine();
}
else
{
sb.AppendLine(sysr.ReadLine());
}
currentLine++;
}
conten = sb.ToString();
this.txtContene.Text += conten;
sysr.Close();
}
continue; 展开
4个回答
展开全部
int intColCount = 0;
bool blnFlag = true;
//定义DataTable
DataTable mydt = new DataTable( "myTableName ");
//定义列
DataColumn mydc;
//定义行
DataRow mydr;
string strpath = " ";
string strline;
string [] aryline;
//读取文件
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);
//只要读取到的内容不是空,就一直读取文件
while((strline = mysr.ReadLine()) != null)
{
//拆分字符串,已 | 为间隔
aryline = strline.Split(new char[]{ '| '});
if (blnFlag)
{
blnFlag = false;
//获取长度
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
//添加列
mydc = new DataColumn(aryline[i]);
//将添加的列加入到DataTable中
mydt.Columns.Add(mydc);
}
}
//新创建行,行的列头、数据类型与DataTable相同
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
// 给新行的数据赋值
mydr[i] = aryline[i];
}
//将新行数据加入到DataTable中
mydt.Rows.Add(mydr);
}
bool blnFlag = true;
//定义DataTable
DataTable mydt = new DataTable( "myTableName ");
//定义列
DataColumn mydc;
//定义行
DataRow mydr;
string strpath = " ";
string strline;
string [] aryline;
//读取文件
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);
//只要读取到的内容不是空,就一直读取文件
while((strline = mysr.ReadLine()) != null)
{
//拆分字符串,已 | 为间隔
aryline = strline.Split(new char[]{ '| '});
if (blnFlag)
{
blnFlag = false;
//获取长度
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
//添加列
mydc = new DataColumn(aryline[i]);
//将添加的列加入到DataTable中
mydt.Columns.Add(mydc);
}
}
//新创建行,行的列头、数据类型与DataTable相同
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
// 给新行的数据赋值
mydr[i] = aryline[i];
}
//将新行数据加入到DataTable中
mydt.Rows.Add(mydr);
}
展开全部
List<string> liststr = new List<string>();
DataTable table = new DataTable();
DataColumn column = null;
// 把列假如到table中
column = new DataColumn("A");
table.Columns.Add(column);
column = new DataColumn("B");
table.Columns.Add(column);
column = new DataColumn("C");
table.Columns.Add(column);
column = new DataColumn("D");
table.Columns.Add(column);
column = new DataColumn("E");
table.Columns.Add(column);
column = new DataColumn("F");
table.Columns.Add(column);
DataRow row;
StreamReader sysr = null;
foreach (string path in liststr)
{
string conten;
int currentLine = 0;
//判断txt文件是否存在
if (File.Exists(path) == true)
{
sysr = new StreamReader(path, UnicodeEncoding.GetEncoding("GBK"));
StringBuilder sb = new StringBuilder();
//读取txt文件,不读第一行数据
while (sysr.Peek() > -1)
{
if (currentLine == 0)
{
sysr.ReadLine();
}
else
{
conten = sysr.ReadLine();
sb.AppendLine(conten);
// 把数据以,号分隔
string[] values = conten.Split(',');
row = table.NewRow();
row["A"] = values[0];
row["B"] = values[1];
row["C"] = values[2];
row["D"] = values[3];
row["E"] = values[4];
row["F"] = values[5];
table.Rows.Add(row);
}
currentLine++;
}
}
continue;
}
if (sysr != null)
{
sysr.Close();
}
DataTable table = new DataTable();
DataColumn column = null;
// 把列假如到table中
column = new DataColumn("A");
table.Columns.Add(column);
column = new DataColumn("B");
table.Columns.Add(column);
column = new DataColumn("C");
table.Columns.Add(column);
column = new DataColumn("D");
table.Columns.Add(column);
column = new DataColumn("E");
table.Columns.Add(column);
column = new DataColumn("F");
table.Columns.Add(column);
DataRow row;
StreamReader sysr = null;
foreach (string path in liststr)
{
string conten;
int currentLine = 0;
//判断txt文件是否存在
if (File.Exists(path) == true)
{
sysr = new StreamReader(path, UnicodeEncoding.GetEncoding("GBK"));
StringBuilder sb = new StringBuilder();
//读取txt文件,不读第一行数据
while (sysr.Peek() > -1)
{
if (currentLine == 0)
{
sysr.ReadLine();
}
else
{
conten = sysr.ReadLine();
sb.AppendLine(conten);
// 把数据以,号分隔
string[] values = conten.Split(',');
row = table.NewRow();
row["A"] = values[0];
row["B"] = values[1];
row["C"] = values[2];
row["D"] = values[3];
row["E"] = values[4];
row["F"] = values[5];
table.Rows.Add(row);
}
currentLine++;
}
}
continue;
}
if (sysr != null)
{
sysr.Close();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-03-10
展开全部
在页面拖一个saveFileDialog1控件;
private void btnJgbc_Click(object sender, EventArgs e)
{
try
{
DataTable myDT = new DataTable();
myDT = GvtoDT(ref myGV);
WriteTxt(myDT);
MessageBox.Show("保存成功!");
}
catch
{
MessageBox.Show("保存失败!");
}
}
private void WriteTxt(DataTable tb)
{
StreamWriter sr;
string report;
if (File.Exists(Application.StartupPath + "//MyFile3.txt")) //如果文件存在,则创建File.AppendText对象
{
sr = File.AppendText(Application.StartupPath + "//MyFile3.txt");
report = "appended";
}
else //如果文件不存在,则创建File.CreateText对象
{
sr = File.CreateText(Application.StartupPath + "//MyFile3.txt");
report = "created";
}
StringBuilder sb = new StringBuilder();
sr.WriteLine("注数/t红1/t红2/t红3/t红4/t红5/t红6/t蓝1/t蓝2/r/n");
foreach (DataRow dr in tb.Rows)
{
sr.WriteLine(dr[0].ToString() + "/t" + dr[1].ToString() + "/t" + dr[2].ToString() + "/t" + dr[3].ToString() + "/t" + dr[4].ToString() + "/t" + dr[5].ToString() + "/t" + dr[6].ToString() + "/t" + dr[7].ToString() + "/r/n");
}
sr.Close();
if( saveFileDialog1.ShowDialog()==DialogResult.OK)
{
File.Copy(Application.StartupPath + "//MyFile3.txt", saveFileDialog1.FileName, true);
File.Delete(Application.StartupPath + "//MyFile3.txt");
}
}
private void btnJgbc_Click(object sender, EventArgs e)
{
try
{
DataTable myDT = new DataTable();
myDT = GvtoDT(ref myGV);
WriteTxt(myDT);
MessageBox.Show("保存成功!");
}
catch
{
MessageBox.Show("保存失败!");
}
}
private void WriteTxt(DataTable tb)
{
StreamWriter sr;
string report;
if (File.Exists(Application.StartupPath + "//MyFile3.txt")) //如果文件存在,则创建File.AppendText对象
{
sr = File.AppendText(Application.StartupPath + "//MyFile3.txt");
report = "appended";
}
else //如果文件不存在,则创建File.CreateText对象
{
sr = File.CreateText(Application.StartupPath + "//MyFile3.txt");
report = "created";
}
StringBuilder sb = new StringBuilder();
sr.WriteLine("注数/t红1/t红2/t红3/t红4/t红5/t红6/t蓝1/t蓝2/r/n");
foreach (DataRow dr in tb.Rows)
{
sr.WriteLine(dr[0].ToString() + "/t" + dr[1].ToString() + "/t" + dr[2].ToString() + "/t" + dr[3].ToString() + "/t" + dr[4].ToString() + "/t" + dr[5].ToString() + "/t" + dr[6].ToString() + "/t" + dr[7].ToString() + "/r/n");
}
sr.Close();
if( saveFileDialog1.ShowDialog()==DialogResult.OK)
{
File.Copy(Application.StartupPath + "//MyFile3.txt", saveFileDialog1.FileName, true);
File.Delete(Application.StartupPath + "//MyFile3.txt");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List<string>
liststr
=
new
List<string>();
DataTable
table
=
new
DataTable();
DataColumn
column
=
null;
//
把列假如到table中
column
=
new
DataColumn("A");
table.Columns.Add(column);
column
=
new
DataColumn("B");
table.Columns.Add(column);
column
=
new
DataColumn("C");
table.Columns.Add(column);
column
=
new
DataColumn("D");
table.Columns.Add(column);
column
=
new
DataColumn("E");
table.Columns.Add(column);
column
=
new
DataColumn("F");
table.Columns.Add(column);
DataRow
row;
StreamReader
sysr
=
null;
foreach
(string
path
in
liststr)
{
string
conten;
int
currentLine
=
0;
//判断txt文件是否存在
if
(File.Exists(path)
==
true)
{
sysr
=
new
StreamReader(path,
UnicodeEncoding.GetEncoding("GBK"));
StringBuilder
sb
=
new
StringBuilder();
//读取txt文件,不读第一行数据
while
(sysr.Peek()
>
-1)
{
if
(currentLine
==
0)
{
sysr.ReadLine();
}
else
{
conten
=
sysr.ReadLine();
sb.AppendLine(conten);
//
把数据以,号分隔
string[]
values
=
conten.Split(',');
row
=
table.NewRow();
row["A"]
=
values[0];
row["B"]
=
values[1];
row["C"]
=
values[2];
row["D"]
=
values[3];
row["E"]
=
values[4];
row["F"]
=
values[5];
table.Rows.Add(row);
}
currentLine++;
}
}
continue;
}
if
(sysr
!=
null)
{
sysr.Close();
}
liststr
=
new
List<string>();
DataTable
table
=
new
DataTable();
DataColumn
column
=
null;
//
把列假如到table中
column
=
new
DataColumn("A");
table.Columns.Add(column);
column
=
new
DataColumn("B");
table.Columns.Add(column);
column
=
new
DataColumn("C");
table.Columns.Add(column);
column
=
new
DataColumn("D");
table.Columns.Add(column);
column
=
new
DataColumn("E");
table.Columns.Add(column);
column
=
new
DataColumn("F");
table.Columns.Add(column);
DataRow
row;
StreamReader
sysr
=
null;
foreach
(string
path
in
liststr)
{
string
conten;
int
currentLine
=
0;
//判断txt文件是否存在
if
(File.Exists(path)
==
true)
{
sysr
=
new
StreamReader(path,
UnicodeEncoding.GetEncoding("GBK"));
StringBuilder
sb
=
new
StringBuilder();
//读取txt文件,不读第一行数据
while
(sysr.Peek()
>
-1)
{
if
(currentLine
==
0)
{
sysr.ReadLine();
}
else
{
conten
=
sysr.ReadLine();
sb.AppendLine(conten);
//
把数据以,号分隔
string[]
values
=
conten.Split(',');
row
=
table.NewRow();
row["A"]
=
values[0];
row["B"]
=
values[1];
row["C"]
=
values[2];
row["D"]
=
values[3];
row["E"]
=
values[4];
row["F"]
=
values[5];
table.Rows.Add(row);
}
currentLine++;
}
}
continue;
}
if
(sysr
!=
null)
{
sysr.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询