SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


OPENROWSET when Excel worksheet name is unknown


OPENROWSET when Excel worksheet name is unknown

Author
Message
bfarley 71690
bfarley 71690
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 4
I've created a stored procedure that accepts two arguments: the name of an Excel workbook (@workbookBillRun), and the name of the first worksheet found in that workbook (@worksheetBillRun).

Through dynamic SQL, I'm able to construct a statement that pulls out data.


SELECT @sqlBillRun = '
SELECT [ACCTNUM], [INVNUM], [RECURRING], [NON RECURRING], [USAGE],[DISCOUNT],[TAXES]
FROM OPENROWSET (''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;;Database=' +@workbookBillRun+ ';HDR=YES'',
''SELECT * from [' +@worksheetBillRun+ '$]'')'


The workbook name will always be known: when the user uploads the file, I change the name to match a certain pattern. Example: 2011-08 AUG.xlsx

However, I won't always know the worksheet name. The workbook *should* contain just one worksheet; and the worksheet *should* be named identically to the workbook (2011-08 AUG), but it may not be named as such. And if I pass an invalid worksheet name, the code above will fail.

I've created error handling to alert the user of an incorrectly named sheet. However, I was curious whether I could extract the name of the first worksheet in the workbook from within T-SQL.

Thanks in advance.
Hansiehans
Hansiehans
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 115
Did you ever got this to work ?
Dana
Dana
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10406 Visits: 3453
Notice that they have not logged in since 2011
Hansiehans
Hansiehans
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 115
Yes, I have seen this...but sometimes the person who asks the question also finds a solution him- or herself.

HOWEVER, for any future finders of this post, I will answer this one ;-)

The key is to dynamically add a (temporary) linked server to the XLSX say e.g. called TempExcelSheet

The you can then query the worksheets and columns using systems stored procedures sp_tables_ex and sp_columns_ex
MMartin1
MMartin1
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16036 Visits: 2080
Hi,
What provider did you use when creating your link?

----------------------------------------------------
How to post forum questions to get the best help
Hansiehans
Hansiehans
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 115
This is the code I used...enjoy :-)

-- Get table (worksheet) or column (field) listings from an excel spreadsheet

DECLARE @linkedServerName sysname = 'TempExcelSpreadsheet'
DECLARE @excelFileUrl nvarchar(1000) = 'D:\\SHARE\\WLAN\\Report.xlsx'

IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable;

-- Remove existing linked server (if necessary)
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end

-- Add the linked server
-- ACE 12.0 seems to work for both xsl and xslx, though some might prefer the older JET provider
exec sp_addlinkedserver
@server = @linkedServerName,
@srvproduct = 'ACE 12.0',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = @excelFileUrl,
@provstr = 'Excel 12.0;HDR=Yes'

-- Grab the current user to use as a remote login
DECLARE @suser_sname NVARCHAR(256) = SUSER_SNAME()

-- Add the current user as a login
EXEC SP_ADDLINKEDSRVLOGIN
@rmtsrvname = @linkedServerName,
@useself = 'false',
@locallogin = @suser_sname,
@rmtuser = null,
@rmtpassword = null

-- Return the table info, each worksheet pbb gets its own unique name
SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;',
'EXEC sp_tables_ex TempExcelSpreadsheet');

SELECT TABLE_NAME
FROM #MyTempTable

--exec sp_executesql 'SELECT * INTO #MyTempTable FROM OPENROWSET(''SQLNCLI'', ''Server=(local);Trusted_Connection=yes;'',''EXEC sp_tables_ex TempExcelSpreadsheet'')'
--EXEC sp_tables_ex @linkedServerName
--EXEC sp_columns_ex @linkedServerName

-- Remove temp linked server
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end
MMartin1
MMartin1
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16036 Visits: 2080
Very awesome. I'll try it out, Thanks!

----------------------------------------------------
How to post forum questions to get the best help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)

Group: General Forum Members
Points: 509141 Visits: 44288
Hansiehans - Friday, June 27, 2014 1:46 AM
This is the code I used...enjoy :-)
-- Get table (worksheet) or column (field) listings from an excel spreadsheet

DECLARE @linkedServerName sysname = 'TempExcelSpreadsheet'
DECLARE @excelFileUrl nvarchar(1000) = 'D:\\SHARE\\WLAN\\Report.xlsx'

IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable;

-- Remove existing linked server (if necessary)
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end

-- Add the linked server
-- ACE 12.0 seems to work for both xsl and xslx, though some might prefer the older JET provider
exec sp_addlinkedserver
@server = @linkedServerName,
@srvproduct = 'ACE 12.0',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = @excelFileUrl,
@provstr = 'Excel 12.0;HDR=Yes'

-- Grab the current user to use as a remote login
DECLARE @suser_sname NVARCHAR(256) = SUSER_SNAME()

-- Add the current user as a login
EXEC SP_ADDLINKEDSRVLOGIN
@rmtsrvname = @linkedServerName,
@useself = 'false',
@locallogin = @suser_sname,
@rmtuser = null,
@rmtpassword = null

-- Return the table info, each worksheet pbb gets its own unique name
SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;',
'EXEC sp_tables_ex TempExcelSpreadsheet');

SELECT TABLE_NAME
FROM #MyTempTable

--exec sp_executesql 'SELECT * INTO #MyTempTable FROM OPENROWSET(''SQLNCLI'', ''Server=(local);Trusted_Connection=yes;'',''EXEC sp_tables_ex TempExcelSpreadsheet'')'
--EXEC sp_tables_ex @linkedServerName
--EXEC sp_columns_ex @linkedServerName

-- Remove temp linked server
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end


I've had this pinned as a link since you posted it and have been seriously remiss in saying "Thank You". Great job (and hat's off to you for the embedded comments that make the code crystal clear) and thanks for sharing even years after the original questions was posted. It's proof positive that all the reminders about how old a post is really don't matter and is definitely worth asking a follow up question or posting an answer even on old posts. Well done and, even though you've not logged on for the previous two months, I hope you're still listening because I think this post has helped a whole lot of people that never took the time to say thanks for solving a fairly common and particularly itchy problem when you're the one that needs a solution.

Shifting gears a bit, this would would make great "SQL Spackle" article. You should write one and submit it. If you want, I'd even be willing to proof it and do a technical review for you.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94157 Visits: 20678
Jeff, are you working up a huge back-log?
Cool
Can we take the Excel thing offline, would like to share some work with you knowing you can write it up much better than I ever could?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)SSC Guru (509K reputation)

Group: General Forum Members
Points: 509141 Visits: 44288
Eirikur Eiriksson - Saturday, December 23, 2017 8:35 AM
Jeff, are you working up a huge back-log?
Cool
Can we take the Excel thing offline, would like to share some work with you knowing you can write it up much better than I ever could?


You should see my presentation on "Automating Excel Hell", which explains how to write "self healing" imports for typical "horizontal" spreadsheets that change every bloody week. Sounds like we may be cut from the same cloth on this subject and I'd love to talk about it. Do you have my private email address?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search