【发布时间】: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