怎么将txt文件中的数据读取到tablewid

如题所述

在页面拖一个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");
}
}
温馨提示:答案为网友推荐,仅供参考