Para aqueles que estão usando o SQL SERVER 2012+, você pode usar o Microsoft OLEDB 12.0 Provider que vem com o SQL Server 2012+ e que permite usar arquivos xlsx do Excel 2007-2013 para consultas distribuídas ad hoc ou como um servidor vinculado. Exemplos abaixo.
A pasta de trabalho do Excel 'Application.xlsx' tem 3 planilhas Application,Device,UserFirst Ativar consultas ad hoc no servidor.
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
Para consultas ad hoc, use a função 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$]');
Para criar um servidor vinculado para o formato 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'
Agora, consulte seu arquivo do Excel de duas maneiras:
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$]