富贵资源网 Design By www.hznty.com
E盘根目录新建一个Excel文件aa.xls后测试如下代码
复制代码 代码如下:
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 (id int , 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' )
/*--测试结果
3 Sheet3
2 Sheet2
1 Sheet1
*/
复制代码 代码如下:
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 (id int , 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' )
/*--测试结果
3 Sheet3
2 Sheet2
1 Sheet1
*/
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...