将电子表格(OpenOffice)导入Visual Basic .net组合框

时间:2017-11-11 03:01:26

标签: vb.net visual-studio import-from-excel

我有一个OpenOffice电子表格中的客户名称,项目名称,员工姓名和小时费率列表,我需要将其导入到Visual Basic .net 2017.如果我可以将其添加到组合框中,因此用户只需从下拉列表中选择名称即可。如果没有设置SQL服务器,似乎这是不可能的。有谁知道我应该怎么做呢?

我试过这个,但它说它无法连接到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 个答案:

答案 0 :(得分: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