C#EXCEL数据导入到ACCESS数据库,该怎么写,最好有例子参考下,

解决方案 »

  1.   

    private DataTable XlsToDataTable(String strpath)   
      {   
      string strConn;   
      strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath + ";" +   
      "Extended Properties=Excel 8.0;";   
      OleDbConnection conn = new OleDbConnection(strConn);   
      OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);   
      DataTable dt = new DataTable();   
      myCommand.Fill(dt);   
      return dt;   
      }   或
    private void button1_Click(object sender, EventArgs e)   
      {   
      string str="";
      oleDbConnExcel = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source=" + Application.StartupPath.Trim() +"\\a.xls");   
      oleDbConnExcel.Open();   
      strGetDataFromExcel = "SELECT * FROM [Sheet1$]";   
      oleDbCmdExcel = new OleDbCommand(strGetDataFromExcel, oleDbConnExcel);   
      oleDbDataReaderExcel = oleDbCmdExcel.ExecuteReader();   
      if (oleDbDataReaderExcel.HasRows == true)   
      {   
      oleDbConnAccess.Open();   
      for (; ; )   
      {   
      if (oleDbDataReaderExcel.Read())   
      {   
      str= "";   
      oleDbCmdAccess = new OleDbCommand(str, oleDbConnAccess);   
      oleDbCmdAccess.ExecuteNonQuery();   
      oleDbCmdAccess.Dispose();   
      }   
      else   
      break;   
      }   
      oleDbConnAccess.Close();   
      }   
      oleDbDataReaderExcel.Close();   
      oleDbCmdExcel.Dispose();   
      oleDbConnExcel.Close();   
      }