Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
davsarg
Starting Member
2 Posts |
Posted - 2011-10-20 : 08:52:03
|
| Hi everyone,I'm trying to find a way of getting the sheet names and, more importantly the sheet IDs from an Excel 2007 (xlsx) Workbook into Sql Server 2008. I do have some code (not written by me) that will work for Excel 2003 but not for Excel 2007. Can anyone offer any suggestions as to how I could accomplish this please . It's worth pointing out that, being something of a newbie to this aspect of SQL, I don't understand how the existing code works. Here's the code as it stands:ALTER PROCEDURE [dbo].[spSelUploadXlsWorkSheets](@xlsWorkbook VARCHAR(255))ASBEGINSET NOCOUNT ONDECLARE @hr int DECLARE @adoCatalogue int DECLARE @adoConn int DECLARE @numTables int -- Number of tables/sheets found in workbookDECLARE @curTable int -- current table/sheet processedDECLARE @tableName VARCHAR(200)SET @curTable = 0-- Table variable to hold all sheetnames in workbookDECLARE @result TABLE (sheetNo SMALLINT, sheetname varchar(255))-- Create an ADOX-Catalog objectEXEC @hr = master.dbo.sp_OACreate 'ADOX.Catalog', @adoCatalogue OUTPUTPRINT 'Create Catalog : ' + CAST (@hr AS VARCHAR)-- Create an ADODB.Connection ObjectEXEC @hr = master.dbo.sp_OACreate 'ADODB.Connection', @adoConn OUTPUTPRINT 'Create Connection : ' + CAST (@hr AS VARCHAR)-- Set Properties to open an Excel WorkbookEXEC @hr = master.dbo.sp_OASetProperty @adoConn, 'Provider', 'Microsoft.Jet.OLEDB.4.0'PRINT 'Add Provider : ' + CAST (@hr AS VARCHAR)EXEC @hr = master.dbo.sp_OASetProperty @adoConn, 'Properties("Extended Properties").Value', 'Excel 8.0'PRINT 'Add extended properties : ' + CAST (@hr AS VARCHAR)PRINT @xlsWorkbookEXEC @hr = master.dbo.sp_OASetProperty @adoConn, 'Properties("Data Source").Value', @xlsWorkbookPRINT 'Create Data Source : ' + CAST (@hr AS VARCHAR)-- Open Connection to WorkbookEXEC @hr = master.dbo.sp_OAMethod @adoConn, 'Open'PRINT 'Open Connection : ' + CAST (@hr AS VARCHAR)-- Return OLE errorDECLARE @src VARCHAR(200)DECLARE @desc VARCHAR(200)EXEC sp_OAGetErrorInfo @adoConn, @src OUT, @desc OUT PRINT convert(varbinary(4),@hr)PRINT @srcPRINT @desc -- Get ActiveConnection ReferenceEXEC @hr = master.dbo.sp_OASetProperty @adoCatalogue, 'ActiveConnection', @adoConnPRINT 'Retrieve Active Connection: ' + CAST (@hr AS VARCHAR)-- Get total count of worksheets and named ranges in workbookEXEC @hr = master.dbo.sp_OAGetProperty @adoCatalogue, 'Tables.count', @numTables OUTPUTPRINT 'Table Count : ' + CAST (@hr AS VARCHAR)-- Loop through all the worksheets to retrieve the nameWHILE @curTable <= (@numTables - 1)BEGINDECLARE @command VARCHAR(200)SET @command = 'Tables(' + CAST(@curTable AS VARCHAR) + ').name'EXEC @hr = master.dbo.sp_OAGetProperty @adoCatalogue, @command, @tableName OUTPUTPRINT 'Table Names : ' + CAST (@hr AS VARCHAR)PRINT 'Table Names : ' + CAST (@hr AS VARCHAR)INSERT INTO @result SELECT @curTable, @tableNameSET @curTable = @curTable + 1ENDEXEC @hr = master.dbo.sp_OAMethod @adoConn, 'Close'EXEC @hr = master.dbo.sp_OADestroy @adoConnSELECT sheetNo, sheetname FROM @resultENDwhen I execute the procedure and pass an appropriate parameter to it, the query executes successfully but with no results and I get the following messages displayed:<<\\ACTUAL SERVER NAME, PATH & FILE NAME WITH XLSX EXTENSION IS DISPLAYED HERE>>>Create Catalog : 0ADO Catalogue : 16711422Create Connection : 0ADODB.Connection : 33488638Add Provider : 0Add extended properties : 0<<\\ACTUAL SERVER NAME, PATH & FILE NAME WITH XLSX EXTENSION IS DISPLAYED HERE>>>Create Data Source : 0Open Connection : -21474672590x80004005Microsoft JET Database EngineExternal table is not in the expected format.Retrieve Active Connection: -2146824579Table Count : 0(1 row(s) affected) If anyone could help me with this then I'd be very grateful.Many thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-11-16 : 05:35:06
|
| EXCEL 2007 version needs a different JET engine. Make sure that the Server has Jet 12.0 engineMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|