-- Get table (worksheet) or column (field) listings from an excel spreadsheet -- set the variable declare @ incluservername sysname = 'tempexcelspreadsheet 'declare @ excelFileUrl nvarchar (1000) = 'd: \ text.xlsx' --/SET -- delete the Link Service (if it already exists) if exists (select null from sys. servers where name = @ linkedServerName) begin exec sp_dropserver @ server = @ linkedServerName, @ droplogins = 'droplogin' end -- add service object -- ACE 12.0 can work well *. xls and *. xlsx, you can also use Jet, but only access *. xls file exec sp_addmediaserver @ server = @ javasservername, @ srvproduct = 'ace 12.0 ', @ provider = 'Microsoft. ACE. OLEDB.12.0 ', @ datasrc = @ excelFileUrl, @ provstr = 'excel 12.0; HDR = yes' -- get the current user declare @ suser_sname nvarchar (256) = suser_sname () -- add the current user as the login service exec sp_addjavassrvlogin @ rmtsrvname = @ linkedServerName, @ useself = 'false', @ locallogin = @ suser_sname, @ rmtuser = null, @ rmtpassword = null -- returns the exec sp_tables_ex @ linkedServerNameexec sp_columns_ex @ linkedServerName -- if exists (select null from sys. servers where name = @ linkedServerName) begin exec sp_dropserver @ server = @ linkedServerName, @ droplogins = 'droplogin' end
Source: http://stackoverflow.com/questions/7439068/get-excel-sheet-name-in-sql-server