【问题标题】:How to import the data from excel to the .mdb access file?如何将数据从 excel 导入 .mdb 访问文件?
【发布时间】:2015-08-25 08:03:28
【问题描述】:

这里我有一些问题是将excel中的数据导出为.mdb的格式。我正在尝试下面显示的代码,但它显示了 messageBox

导入失败,请更正工作表中的列名!

错误信息:

“Microsoft.Jet.OLEDB.4.0”提供程序未在本地计算机上注册

有没有人可以帮助我。

最好的问候, 领主

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' delete the file with the same and create a new access file
    If File.Exists("C:\Users\Admin\Desktop\test\CA\book.mdb") Then
        File.Delete("C:\Users\Admin\Desktop\test\CA\book.mdb")
    End If

    Dim _accessData As Access.Application
    _accessData = New Access.Application()
    _accessData.Visible = False
    _accessData.NewCurrentDatabase("C:\Users\Admin\Desktop\test\CA\book.mdb", Access.AcNewDatabaseFormat.acNewDatabaseFormatAccess2000, , , )

    _accessData.CloseCurrentDatabase()
    _accessData.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll)
    _accessData = Nothing

    ' initialize the connect string
    Dim _filename As String = "C:\Users\Admin\Desktop\test\CA\test.xls"
    Dim _conn As String
    _conn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\Admin\Desktop\test\CA\test.xls" & _filename & ";" & "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

    Dim _connection As OleDbConnection = New OleDbConnection(_conn)

    'Use OledbCommand object to select all the data from sheet1 and execute an ExecuteNonQuery to import data into Book.mdb.
    Dim _command As OleDbCommand = New OleDbCommand()
    _command.Connection = _connection

    Try
        _command.CommandText = "SELECT * INTO [MS Access;Database=C:\Users\Admin\Desktop\test\CA\book.mdb].[Sheet1] FROM [Sheet1$A4:D]"
        _connection.Open()
        _command.ExecuteNonQuery()
        _connection.Close()
        MessageBox.Show("The import is complete!")

    Catch e1 As Exception
        MessageBox.Show("Import Failed, correct Column name in the sheet!" & Environment.NewLine & "Error Message:" & Environment.NewLine & e1.Message)
    End Try

End Sub

*注意:程序可以创建.mdb文件,但不能导入excel数据。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    我很快就解决了……谢谢你的帮助……

    Public Class Form1
        Private DBPath As String
        Private conn As OleDbConnection
        Public Sub New()
            InitializeComponent()
        End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ''delete the file with the same and create a new access file
            If File.Exists("C:\Users\Inari Admin\Desktop\test\CA\test.mdb") Then
                File.Delete("C:\Users\Inari Admin\Desktop\test\CA\test.mdb")
            End If
    
    DBPath = "C:\Users\Inari Admin\Desktop\test\CA\test.mdb"
    
            ' create DB via ADOX if not exists
            ' NOTE: to use ADOX add reference to COM Microsoft ADO Ext. 2.6 for DDL and Security!
            If Not File.Exists(DBPath) Then
                Dim cat As New ADOX.Catalog()
                cat.Create(Convert.ToString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & DBPath)
                cat = Nothing
    
            End If
    
            conn = New OleDbConnection(Convert.ToString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & DBPath)
            conn.Open()
    
            Try
                Using cmd As New OleDbCommand("CREATE TABLE [test] ([id] COUNTER PRIMARY KEY, [num] INT, [name] MEMO, [no] INT);", conn)
                    cmd.ExecuteNonQuery()
                End Using
    
            Catch ex As Exception
                If ex IsNot Nothing Then
                    ex = Nothing
    
                End If
            End Try
    
            ' initialize the connect string
            Dim _filename As String = "C:\Users\Inari Admin\Desktop\test\CA\test.xls"
            Dim _conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _filename & ";" & "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
            Dim _connection As OleDbConnection = New OleDbConnection(_conn)
    
            'Use OledbCommand object to select all the data from sheet1 and execute an ExecuteNonQuery to import data into test.mdb.
            Dim _command As OleDbCommand = New OleDbCommand()
            _command.Connection = _connection
    
            Try
                Using conn As New OleDbConnection(_conn)
                    Using cmd As New OleDbCommand()
                        cmd.Connection = conn
                        cmd.CommandText = "INSERT INTO [MS Access;Database=" + DBPath + "].[test] SELECT * FROM [Sheet1$]"
                        conn.Open()
                        cmd.ExecuteNonQuery()
                    End Using
                End Using
                '_command.CommandText = "SELECT * FROM [Sheet1$]"
                '_connection.Open()
                '_command.ExecuteNonQuery()
                '_connection.Close()
                MessageBox.Show("The import is complete!")
    
            Catch e1 As Exception
                MessageBox.Show("Import Failed, correct Column name in the sheet!" & Environment.NewLine & "Error Message:" & Environment.NewLine & e1.Message)
            End Try
    
        End Sub
    
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多