各位大侠好!
晚辈最进遇到一个棘手的问题!向大家请教!
—————————————————————————————————————————————
如何把excel文件的逐条记录添加到我的数据表中?
如:有excel数据表
| id | name |
.............n多记录要把这些记录全部添加到我数据库的一个数据表中
如:
| id | name |
—————————————————————————————————————————————
该操作要如何来写!
晚辈对如何控制读取excel文件的操作不熟悉,请各位大侠帮个忙!
—————————————————————————————————————————————
跪谢!!跪谢!!

解决方案 »

  1.   

    Dim DS As System.Data.DataSet
    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    Dim MyConnection As System.Data.OleDb.OleDbConnectionMyConnection = New System.Data.OleDb.OleDbConnection( _
          "provider=Microsoft.Jet.OLEDB.4.0; " & _
          "data source=C:\myData.XLS; " & _
          "Extended Properties=Excel 8.0;")
    ' Select the data from Sheet1 of the workbook.
    MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
          "select * from [Sheet1$]", MyConnection)DS = New System.Data.DataSet()
    MyCommand.Fill(DS)
    MyConnection.Close()
      

  2.   

    SqlConnection myConnection = new SqlConnection("……");
        SqlCommand myCommand = new SqlCommand();
        SqlTransaction myTrans;    // Open the connection.
        myConnection.Open();    // Assign the connection property.
        myCommand.Connection  = myConnection;    // Begin the transaction.
        myTrans = myConnection.BeginTransaction();    // Assign transaction object for a pending local transaction
        myCommand.Transaction = myTrans;    try
        {
            strDataSource = "'Microsoft.Jet.OLEDB.4.0','Data Source=c:\123.excel;User ID=Admin;Password=;Extended properties=Excel 8.0'";
            myCommand.CommandText = " INSERT INTO table1 Select * FROM OpenDataSource(" + strDataSource + ")...Sheet1$";
            myCommand.ExecuteNonQuery();
        }
        catch(Exception e)
        {
          myTrans.Rollback();
         
        }
        finally
        {
          myConnection.Close();
        }哈哈,把其中几个地方改为你所需要的名字的就ok了!
      

  3.   

    http://www.cnblogs.com/renyu732/archive/2005/06/15/174866.html
      

  4.   

    swell624(心青) 的方法就可以呀,不过还要加上一句
    myTrans.Commit();
    才可以.
      

  5.   

    OpenDataSource(" + strDataSource + ")...Sheet1$Sheet1$
    是什么东东啊?