Reading values from an Excel File

后端 未结 5 1573
無奈伤痛
無奈伤痛 2021-01-19 03:09

I want to get a value from 12 excel sheet. is there any way that i get the values without opening the excel sheet? I am using vb.net. Please post an example code, if there i

5条回答
  •  天涯浪人
    2021-01-19 03:37

    use this code for that,

    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
    

提交回复
热议问题