【问题标题】:Import Excel data to SQL Server将 Excel 数据导入 SQL Server
【发布时间】:2011-09-26 23:31:44
【问题描述】:
我想将 excel 文件中的数据插入 SQL Server..
我使用了以下 SQL 语句:
SELECT *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=Y:\Path.xls',
'SELECT * FROM [Sheet$]')
但我收到以下错误 -
消息 7403,第 16 级,状态 1,第 1 行
OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”尚未注册。
【问题讨论】:
标签:
sql
sql-server
excel
import
openrowset
【解决方案1】:
您可以使用以下代码从 Excel 中导入 SQL 中的数据。只是你需要明白你必须在 VBA 中添加一个库,然后你才会使用下面的代码。
进入 VBA,然后添加引用 - Microsoft ActiveX Data objects 2.8 Library。
Sub sbADOExample()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = "SELECT * From [Sheet1$]" ' Your SQL Statement (Table Name= Sheet Name=[Sheet1$])
mrs.Open sSQLSting, Conn
'=>Load the Data into an array
'ReturnArray = mrs.GetRows
''OR''
'=>Paste the data into a sheet
Sheet2.Range("A2").CopyFromRecordset mrs
'Close Recordset
mrs.Close
'Close Connection
Conn.Close
End Sub
【解决方案2】:
尝试运行这个:
USE [tableName]
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
在您的查询中
并使用 Microsoft.ACE.OLEDB.12.0 而不是 "Microsoft.Jet.OLEDB.4.0" ,对我有用
【解决方案4】:
您可以改用 SQL Server 的导入数据向导。