How to read Excel cell from VB.Net

后端 未结 3 1219
失恋的感觉
失恋的感觉 2020-12-19 10:11

How can I read specific cell from Excel file using OLEDB Connection with VB.NET?

Can you show me sample code?

3条回答
  •  半阙折子戏
    2020-12-19 10:29

    try this c# code,

    DimobjEXCELCon As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EXCLE_FILE_PATH;Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
    ExcelConnection.Open()
    
    Dim objQuery As String = "SELECT * FROM [Sheet1$]" 'get values from sheet1, here you can change your sheet name
    
    Dim objCMD As OleDbCommand = New OleDbCommand(objQuery,objEXCELCon)
    Dim objDR As OleDbDataReader
    
    Dim SQLconn As New SqlConnection()
    Dim szCON As String = "Connection string for database"
    SQLconn.ConnectionString = szCON
    SQLconn.Open()
    
    
    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLConn)
    bulkCopy.DestinationTableName = "TableToWriteToInSQLSERVER"
    
     Try
      objDR = objCMD.ExecuteReader
      bulCopy.WriteToServer(objDR)
      objDR.Close()
      SQLConn.Close()
    
     Catch ex As Exception
      MsgBox(ex.ToString)
     End Try
    

提交回复
热议问题