简体   繁体   English

将Excel数据导入SQL服务器

[英]Import Excel data to SQL Server

I want to insert the data from excel file into the SQL Server.. I used the following SQL Statement:我想将 excel 文件中的数据插入 SQL 服务器。我使用了以下 SQL 语句:

SELECT * 
  FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                  'Excel 8.0;Database=Y:\Path.xls',
                  'SELECT * FROM [Sheet$]')

But i am getting the following error -但我收到以下错误 -

Msg 7403, Level 16, State 1, Line 1消息 7403,第 16 级,State 1,第 1 行
The OLE DB provider "Microsoft.Jet.OLEDB.4.0" has not been registered. OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”尚未注册。

You can use Import data wizard of SQL Server instead.您可以改用 SQL 服务器的导入数据向导。

Try running this:尝试运行这个:

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

in your query在您的查询中

and use Microsoft.ACE.OLEDB.12.0 instead of "Microsoft.Jet.OLEDB.4.0", works for me并使用 Microsoft.ACE.OLEDB.12.0 而不是“Microsoft.Jet.OLEDB.4.0”,对我有用

You can use below code to import the data in SQL from Excel.您可以使用以下代码从 Excel 导入 SQL 中的数据。 Just you need to understand that you have to add a library in VBA and only then you will use below code.只是你需要明白你必须在 VBA 中添加一个库,然后你才会使用下面的代码。 Go in VBA and then add reference – Microsoft ActiveX Data objects 2.8 Library. VBA 中的 Go 然后添加引用 - Microsoft ActiveX 数据对象 2.8 库。

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

If you are using SQL Server version>=2005 you can use management studio or SSIS.如果您使用的是 SQL 服务器版本>=2005,您可以使用管理工作室或 SSIS。

check the link http://msdn.microsoft.com/en-us/library/ms140052(v=SQL.90).aspx检查链接http://msdn.microsoft.com/en-us/library/ms140052(v=SQL.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM