0

最初我使用 Office Interop 来导入数据,但这对我和我的计算机来说都是一个令人头疼的问题。现在我正在尝试用 ACE 加载它,但我的数据网格没有被填充。一旦启动并运行,我需要知道如何以其他方式使用该数据,以及如何从该 DataSet 中获取特定的数据单元格。顺便说一句,我正在使用 Visual Studio 2008。

现在我有...

Public Function funcUpdate(ByVal sFileLoc As String) As Boolean
    'Determine connection string properties
    Dim dbProperty As String
    If updFileExt = ".xlsx" Then
        dbProperty = "Excel 12.0 Xml;HDR=No"
    ElseIf updFileExt = ".xls" Then
        dbProperty = "Excel 12.0;HDR=No"
    Else
        MessageBox.Show("FATAL: File type error on updater", "OHGAWDNO", MessageBoxButtons.OK, MessageBoxIcon.Error)
        updateTerm()
        Return False
    End If
    Dim dbConn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & updFile & ";Extended Properties=" & dbProperty & ";")
    Dim dbCommand As New OleDb.OleDbDataAdapter("select * from [sheet1$]", dbConn)
    Dim dtSet As New DataSet
    Try
        dbCommand.TableMappings.Add("Table", "ExcelTest")
        dbCommand.Fill(dtSet)
        Form1.DataGrid1.DataSource = dtSet.Tables(0)
    Catch exlErr As Exception
    Finally
        dbConn.Close()
    End Try

    updateTerm()
End Function
4

1 回答 1

0

尝试这样做。

Dim path As String = "c:\example.xlsx"
Dim constr As String = [String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;" & _
                                           "HDR=YES;IMEX=1;""", path)
Dim adapter As New OleDbDataAdapter
Using cn As New System.Data.OleDb.OleDbConnection(constr)

        Try
            cmdselcet = New OleDbCommand("SELECT * FROM [Sheet1$]", cn)
            cn.Open()

            adapter.SelectCommand = cmdselcet
            Dim ds As DataSet
            ds = New DataSet

            'Display
            adapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            MsgBox("Done!")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Using
于 2015-02-02T12:09:11.417 回答