疯狂源代码吧 关注:12,744贴子:34,933
  • 0回复贴,共1

求大神解决

只看楼主收藏回复

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog op1 = new OpenFileDialog();
op1.ShowDialog();
Import(op1.FileName);
}
// <summary>
/// 导入excel数据
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public void Import(string filePath)
{
try
{
//Excel就好比一个数据源一般使用
//这里可以根据判断excel文件是03的还是07的,然后写相应的连接字符串
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 15.0;HDR=Yes;IMEX=1;'";
OleDbConnection con = new OleDbConnection(strConn);
con.Open();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
DataSet ds = new DataSet();
odda.Fill(ds);
for (int i = 2; i < ds.Tables[0].Rows.Count; i++)
{
string StuName = ds.Tables[0].Rows[i][1].ToString();
string StuSex = ds.Tables[0].Rows[i][1].ToString();
string StuPhone = ds.Tables[0].Rows[i][1].ToString();
string strsql = "insert into Student(StuName,StuSex,StuPhone) values('" + StuName + "','" + StuSex + "','" +StuPhone+ "') ";
DBhelp.GetDs(strsql);
}
}
catch (Exception ex)
{
MessageBox.Show("导入失败");
}
}
弹出导入失败,那里错了


IP属地:广东1楼2015-05-05 17:10回复