2017-11-11 89 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