日期:2014-05-17 浏览次数:20757 次
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string FileName = this.openFileDialog1.FileName;
char fenge = '-';
string strErrorMessage = "出错";
dataGridView1.DataSource = TxtToDataTable(FileName, fenge, ref strErrorMessage);
int count = dataGridView1.RowCount - 1;
lblCount.Text = "共导入" + count + "条数据";
// 你的 处理文件路径代码
}
}
public static DataTable TxtToDataTable(string strFileName, char strSplit, ref string strErrorMessage)
{
DataTable dtReturn = new DataTable();
try
{
string[] strFileTexts = File.ReadAllLines(strFileName);
if (strFileTexts.Length == 0)
{
strErrorMessage = "文件中没有数据!";
return null;
}
string[] strLineTexts = strFileTexts[0].Split('-');
if (strLineTexts.Length == 0)
{
strErrorMessage = "文件中数据格式不正确!";
return null;
&nb