简体   繁体   English

如何在Excel Server中将Excel文件配置为链接服务器?

[英]How do I configure an Excel file as a Linked Server in SQL Server?

I have opened the Linked Server dialog to create a Linked Server so that I can import my excel file data to a SQL Server 2005 database. 我已打开“链接服务器”对话框以创建“链接服务器”,以便可以将Excel文件数据导入到SQL Server 2005数据库中。 Which provider must I use and what other settings do I need to fill in? 我必须使用哪个提供商,还需要填写哪些其他设置?

For those who are using SQL SERVER 2012+ you can use the Microsoft OLEDB 12.0 Provider that comes with SQL Server 2012+ and which allows you to use Excel 2007-2013 xlsx files for adhoc distributed queries or as a linked server. 对于使用SQL SERVER 2012+的用户,您可以使用SQL Server 2012+附带的Microsoft OLEDB 12.0提供程序,该提供程序允许您将Excel 2007-2013 xlsx文件用于即席分布式查询或用作链接服务器。 Examples below. 下面的例子。

The Excel workbook 'Application.xlsx' has 3 worksheets Application,Device,User First Activate Ad Hoc Queries on the Server. Excel工作簿'Application.xlsx'具有3个工作表,分别是应用程序,设备,用户首先激活服务器上的临时查询。

USE MSDB
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE WITH OverRide
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

For Ad Hoc Queries use the OPENROWSET Function. 对于临时查询,请使用OPENROWSET函数。

SELECT * FROM 
OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel   8.0;Database=C:\Users\Administrator\Desktop\Application.xlsx;HDR=YES', 'SELECT * FROM [Application$]');

SELECT * FROM 
OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel   8.0;Database=C:\Users\Administrator\Desktop\Application.xlsx;HDR=YES', 'SELECT * FROM [Device$]');

SELECT * FROM 
OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel  8.0;Database=C:\Users\Administrator\Desktop\Application.xlsx;HDR=YES', 'SELECT * FROM [User$]');

For Creating a Linked Server for Excel 2007-2013 format: 对于为Excel 2007-2013格式创建链接服务器:

USE MSDB
GO
EXEC sp_addLinkedServer
@server= 'XLSX_MATRIX',
@srvproduct = 'ACE 12.0',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = 'C:\Users\Administrator\Desktop\Application.xlsx',
@provstr = 'Excel 12.0; HDR=Yes'

Now, query your excel file in two ways: 现在,以两种方式查询您的excel文件:

SELECT * FROM OPENQUERY (XLSX_MATRIX, 'Select * from [Application$]')
SELECT * FROM OPENQUERY (XLSX_MATRIX, 'Select * from [Device$]')
SELECT * FROM OPENQUERY (XLSX_MATRIX, 'Select * from [User$]')

SELECT * FROM XLSX_MATRIX...[Application$]
SELECT * FROM XLSX_MATRIX...[Device$]
SELECT * FROM XLSX_MATRIX...[User$]

You would set it up using either OLEDB provider or the provider for ODBC drivers and create a connection using the ODBC Administrator tool on the server to the Excel file. 您可以使用OLEDB提供程序或ODBC驱动程序提供程序进行设置,并使用服务器上的ODBC管理器工具创建与Excel文件的连接。 Are you planning to read from this Excel file on a regular basis? 您是否打算定期读取此Excel文件? If not, then setting it up as a Linked Server could be overkill. 如果没有,那么将其设置为链接服务器可能会显得过分。

For more details, see " How to import data from Excel to SQL Server " and " How to use Excel with SQL Server linked servers and distributed queries " 有关更多详细信息,请参见“ 如何将数据从Excel导入SQL Server ”和“ 如何将Excel与SQL Server链接的服务器和分布式查询一起使用

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

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