How to parse SQLServer to obtain all Sheet in Excel

Source: Internet
Author: User

Create an excelfile aa.xls in the einventory directory and test the following code:
Copy codeThe Code is as follows:
Use tempdb
Go
If (object_id ('udf _ getExcelTableNames ') is not null)
Drop function dbo. udf_getExcelTableNames
Go
Create function udf_getExcelTableNames (@ filename varchar (1000 ))
Returns @ t table (idint, name varchar (255 ))
As
Begin
Declare
@ Error int, @ obj int, @ c int, @ sheetname varchar (255), @ sheetstring varchar (255)

Exec @ error = sp_oacreate 'excel. application', @ obj out
Exec @ error = sp_oamethod @ obj, 'workbooks. open', @ c out, @ filename
Exec @ error = sp_oagetproperty @ obj, 'activeworkbook. Sheets. count', @ c out
While (@ c> 0)
Begin
Set @ sheetstring = 'activeworkbook. Sheets ('+ ltrim (@ c) +'). name'
Exec @ error = sp_oagetproperty @ obj, @ sheetstring, @ sheetname out
Insert into @ t select @ c, @ sheetname
Set @ c = @ c-1
End
Exec @ error = sp_oadestroy @ obj
Return
End
Go
Select * from dbo. udf_getExcelTableNames ('e:/aa.xls ')
/* -- Test Result
3 Sheet3
2 Sheet2
1 Sheet1
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.