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;
}
//for (int i = 0; i < strLineTexts.Length; i++)
//{
// dtReturn.Columns.Add("Columns" + i.ToString());
//}
dtReturn.Columns.Add("账号");
dtReturn.Columns.Add("密码");
dtReturn.Columns.Add("注册状态");
for (int i = 0; i < strFileTexts.Length; i++)
{
strLineTexts = strFileTexts[i].Replace("----","-").Split('-');
DataRow dr = dtReturn.NewRow();
for (int j = 0; j < strLineTexts.Length; j++)
{
dr[j] = strLineTexts[j].ToString();
}
dtReturn.Rows.Add(dr);
}
}
catch (Exception ex)
{
strErrorMessage = "读入数据出错!" + ex.Message; return null;
} return dtReturn;
}
上面的代码,导入自己填写的格式就没错,导入 库里的就导入不进去,求大神指导。。能导入的文本下载这个 http://pan.baidu.com/share/link?shareid=393658&uk=1026065227不能导入是这个,下载下,http://pan.baidu.com/share/link?shareid=393660&uk=1026065227
{
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;
}
//for (int i = 0; i < strLineTexts.Length; i++)
//{
// dtReturn.Columns.Add("Columns" + i.ToString());
//}
dtReturn.Columns.Add("账号");
dtReturn.Columns.Add("密码");
dtReturn.Columns.Add("注册状态");
for (int i = 0; i < strFileTexts.Length; i++)
{
strLineTexts = strFileTexts[i].Replace("----","-").Split('-');
DataRow dr = dtReturn.NewRow();
for (int j = 0; j < strLineTexts.Length; j++)
{
dr[j] = strLineTexts[j].ToString();
}
dtReturn.Rows.Add(dr);
}
}
catch (Exception ex)
{
strErrorMessage = "读入数据出错!" + ex.Message; return null;
} return dtReturn;
}
上面的代码,导入自己填写的格式就没错,导入 库里的就导入不进去,求大神指导。。能导入的文本下载这个 http://pan.baidu.com/share/link?shareid=393658&uk=1026065227不能导入是这个,下载下,http://pan.baidu.com/share/link?shareid=393660&uk=1026065227
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货