2017-11-11 105 views
-2

我在OpenOffice電子表格中列出了客戶端名稱,項目名稱,員工姓名和小時費率,我需要將其導入到Visual Basic .net 2017中。如果我可以對組合框執行此操作,將是首選,因此用戶可以從下拉列表中選擇名稱。如果不設置SQL服務器,這似乎是不可能的。有誰知道我應該怎麼做呢?將輸入電子表格(OpenOffice)導入到Visual Basic .net組合框中

我已經試過,但它說,它無法連接到Microsoft.Ace.OLEDB.12.0 我從YouTube視頻

Private Sub btnGetSpread_Click(sender As Object, e As EventArgs) Handles btnGetSpread.Click 
    Try 

     Dim MyConnection As System.Data.OleDb.OleDbConnection 
     Dim dataSet As System.Data.DataSet 
     Dim MyCommand As System.Data.OleDb.OleDbDataAdapter 
     Dim path As String = "P:\Coding\Visual Studio\Visual Basic\TestProject\TestProject\bin\Files\Company_Sheet.ods" 

     MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source =" + "P:\Coding\Visual Studio\Visual Basic\TestProject\TestProject\bin\Files\Company_Sheet.ods" + ";Extended Properties=Excel 12.0;") 
     MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection) 

     dataSet = New System.Data.DataSet 
     MyCommand.Fill(dataSet) 
     dgvSpread.DataSource = dataSet.Tables(0) 

     MyConnection.Close() 

    Catch ex As Exception 

     MsgBox(ex.Message.ToString) 

    End Try 

End Sub 
+1

你有什麼試過自己? – Ibo

+1

在發帖之前閱讀[問]並參加[旅遊]。另外,這些標籤中的幾個是互斥的。標籤包含指導文字。 – Plutonix

+0

這與'excel'和'vba'有什麼關係? – jsotola

回答

0

這將讓你開始得到這個代碼。我有時不得不將數據從Excel電子表格導入到Oracle數據庫中,這個代碼已經有幾年了,但仍然有效(VS 2013)。修改以適應您的情況。

Dim sourceFile As String = "C:\Users\appdata\Documents\SomeData.xls" 
    Dim srcConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourceFile & ";Extended Properties=""Excel 8.0;HDR=YES;""" 
    Dim srcConn As OleDbConnection = New OleDbConnection(srcConnString) 
    srcConn.Open() 
    ' in the select statement, the fields are the column names in the spreadsheet and are expected to be in row 1 and are case sensitive 
    ' the from clause in the query is the tab of the spreadsheet where the data is 
    Dim cmdExcel As OleDbCommand = New OleDbCommand("Select NAME,ADDRESS,CITY,STATE,ZIP From [DATA$] Where some where clause", srcConn) 
    Dim drExcel As OleDbDataReader = cmdExcel.ExecuteReader() 
    ' Now loop through the rows in the spreadsheet 
    While drExcel.Read() 
     'Do stuff ... 
    End While