Resolve SQL Server to get all the sheet in Excel

Source: Internet
Author: User
Tags directory create

E Packing directory Create a new Excel file Aa.xls and test the following code

Copy Code code as follows:


Use tempdb
Go
if (object_id (' udf_getexceltablenames ') are NOT null)
Drop function dbo. udf_getexce Ltablenames
Go
Create function udf_getexceltablenames (@filename varchar (1000))
Returns @t table (id int, Name varchar (255))
as
begin
Declare
@error int, @obj int, @c int, @sheetname varchar (255), @sh eetstring 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. UD F_getexceltablenames (' E:/aa.xls ')
/*--test Results
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.