還可以用SQL存儲過程USE [MES] GO /****** Object: StoredProcedure [dbo].[H_SqlToExcel] Script Date: 01/22/2009 17:13:47 ******/ SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO ALTER PROC [dbo].[H_SqlToExcel] ( @Path varchar(100),--文件存放路径 @Fname varchar(100),--文件名字 @SheetName varchar(80),---工作表名字 @SqlStr varchar(8000)--查询语句,如果查询语句中使用了order by ,请加上top 100 percent,注意,如果导出表/视图,用上面的存储过程 ) AS SET NOCOUNT ONdeclare @sql varchar(8000) declare @obj int--OLE对象 declare @constr varchar(8000) declare @err int declare @out int declare @fdlist varchar(8000) declare @tbname sysname--临时表 declare @Src nvarchar(200) declare @Desc nvarchar(200)set @tbname='##tmp_'+convert(varchar(38),newid())exec('select * into ['+@tbname +'] from '+'('+@sqlStr+') A')select @fdlist = ''set @sql= @path+@fname set @constr='DRIVER={Microsoft Excel Driver (*.xls)};DSN='''';READONLY=FALSE' +';CREATE_DB="'+@sql+'";DBQ='+@sql--生成Excel的列 set @sql = '' select @sql = @sql+','+'['+a.name+'] '+ case when b.name like '%char' then case when a.length >255 then 'memo' else 'text('+cast(a.length as varchar)+')' end when b.name like '%int' or b.name='bit' then 'int' when b.name like '%datetime' then 'datetime' when b.name like '%money' then 'money' when b.name like '%text' then 'memo' else b.name end, @fdlist = @fdlist+','+'['+a.name+']' from tempdb..syscolumns a join tempdb..systypes b on a.xtype = b.xusertype where b.name not in('image','uniqueidentifier','sql_variant','varbinary','binary','timestamp') and id in(select id from tempdb..sysobjects where name = @tbname) order by colorder if @@rowcount=0 returnset @fdlist = substring(@fdlist,2,8000)--连接数据库 exec @err=sp_oacreate 'adodb.connection',@obj out if @err <> 0 goto lberror exec @err=sp_oamethod @obj,'open',null,@constr if @err <> 0 goto lberror --创建工作薄 select @sql='create table ['+@sheetname +']('+substring(@sql,2,8000)+')'exec @err=sp_oamethod @obj,'execute',@out out,@sql--@sql为excute方法提供参数if @err <> 0 goto lberrorexec @err=sp_oadestroy @obj--导入数据 set @sql='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES ;DATABASE='+@path+@fname+''',['+@sheetname+'$])' --print @sql exec ('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from ['+@tbname+']')exec('drop table ['+@tbname+']') return lberror: exec sp_oageterrorinfo 0,@src out,@desc outlbexit: select cast(@err as varbinary(4)) as 错误号 ,@src as 错误源,@desc as 错误描述 select @sql,@constr,@fdlist
EXEC master..xp_cmdshell 'bcp TEST.dbo._yyy out c:\Temp.xls -c -q -S"172.20.104.111" -U"sa" -P"13712356517"'
output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NULL
開始複製...
1000 列成功地大量複製到主機檔案。總共接收: 1000
1000 列成功地大量複製到主機檔案。總共接收: 2000
1000 列成功地大量複製到主機檔案。總共接收: 3000
1000 列成功地大量複製到主機檔案。總共接收: 4000
1000 列成功地大量複製到主機檔案。總共接收: 5000
1000 列成功地大量複製到主機檔案。總共接收: 6000
1000 列成功地大量複製到主機檔案。總共接收: 7000
1000 列成功地大量複製到主機檔案。總共接收: 8000
1000 列成功地大量複製到主機檔案。總共接收: 9000
1000 列成功地大量複製到主機檔案。總共接收: 10000
1000 列成功地大量複製到主機檔案。總共接收: 11000
1000 列成功地大量複製到主機檔案。總共接收: 12000
1000 列成功地大量複製到主機檔案。總共接收: 13000
1000 列成功地大量複製到主機檔案。總共接收: 14000
1000 列成功地大量複製到主機檔案。總共接收: 15000
1000 列成功地大量複製到主機檔案。總共接收: 16000
1000 列成功地大量複製到主機檔案。總共接收: 17000
1000 列成功地大量複製到主機檔案。總共接收: 18000
1000 列成功地大量複製到主機檔案。總共接收: 19000
1000 列成功地大量複製到主機檔案。總共接收: 20000
1000 列成功地大量複製到主機檔案。總共接收: 21000
1000 列成功地大量複製到主機檔案。總共接收: 22000
1000 列成功地大量複製到主機檔案。總共接收: 23000
1000 列成功地大量複製到主機檔案。總共接收: 24000
1000 列成功地大量複製到主機檔案。總共接收: 25000
1000 列成功地大量複製到主機檔案。總共接收: 26000
1000 列成功地大量複製到主機檔案。總共接收: 27000
1000 列成功地大量複製到主機檔案。總共接收: 28000
1000 列成功地大量複製到主機檔案。總共接收: 29000
1000 列成功地大量複製到主機檔案。總共接收: 30000
1000 列成功地大量複製到主機檔案。總共接收: 31000
1000 列成功地大量複製到主機檔案。總共接收: 32000
1000 列成功地大量複製到主機檔案。總共接收: 33000
1000 列成功地大量複製到主機檔案。總共接收: 34000
1000 列成功地大量複製到主機檔案。總共接收: 35000
1000 列成功地大量複製到主機檔案。總共接收: 36000
1000 列成功地大量複製到主機檔案。總共接收: 37000
1000 列成功地大量複製到主機檔案。總共接收: 38000
1000 列成功地大量複製到主機檔案。總共接收: 39000
1000 列成功地大量複製到主機檔案。總共接收: 40000
1000 列成功地大量複製到主機檔案。總共接收: 41000
1000 列成功地大量複製到主機檔案。總共接收: 42000
1000 列成功地大量複製到主機檔案。總共接收: 43000
1000 列成功地大量複製到主機檔案。總共接收: 44000
1000 列成功地大量複製到主機檔案。總共接收: 45000
1000 列成功地大量複製到主機檔案。總共接收: 46000
1000 列成功地大量複製到主機檔案。總共接收: 47000
1000 列成功地大量複製到主機檔案。總共接收: 48000
1000 列成功地大量複製到主機檔案。總共接收: 49000
1000 列成功地大量複製到主機檔案。總共接收: 50000
1000 列成功地大量複製到主機檔案。總共接收: 51000
1000 列成功地大量複製到主機檔案。總共接收: 52000
1000 列成功地大量複製到主機檔案。總共接收: 53000
1000 列成功地大量複製到主機檔案。總共接收: 54000
1000 列成功地大量複製到主機檔案。總共接收: 55000
1000 列成功地大量複製到主機檔案。總共接收: 56000
1000 列成功地大量複製到主機檔案。總共接收: 57000
1000 列成功地大量複製到主機檔案。總共接收: 58000
1000 列成功地大量複製到主機檔案。總共接收: 59000
1000 列成功地大量複製到主機檔案。總共接收: 60000
1000 列成功地大量複製到主機檔案。總共接收: 61000
1000 列成功地大量複製到主機檔案。總共接收: 62000
1000 列成功地大量複製到主機檔案。總共接收: 63000
1000 列成功地大量複製到主機檔案。總共接收: 64000
1000 列成功地大量複製到主機檔案。總共接收: 65000
1000 列成功地大量複製到主機檔案。總共接收: 66000
1000 列成功地大量複製到主機檔案。總共接收: 67000
1000 列成功地大量複製到主機檔案。總共接收: 68000
1000 列成功地大量複製到主機檔案。總共接收: 69000
1000 列成功地大量複製到主機檔案。總共接收: 70000
1000 列成功地大量複製到主機檔案。總共接收: 71000
1000 列成功地大量複製到主機檔案。總共接收: 72000
1000 列成功地大量複製到主機檔案。總共接收: 73000
1000 列成功地大量複製到主機檔案。總共接收: 74000
1000 列成功地大量複製到主機檔案。總共接收: 75000
1000 列成功地大量複製到主機檔案。總共接收: 76000
1000 列成功地大量複製到主機檔案。總共接收: 77000
1000 列成功地大量複製到主機檔案。總共接收: 78000
1000 列成功地大量複製到主機檔案。總共接收: 79000
1000 列成功地大量複製到主機檔案。總共接收: 80000
1000 列成功地大量複製到主機檔案。總共接收: 81000
1000 列成功地大量複製到主機檔案。總共接收: 82000
1000 列成功地大量複製到主機檔案。總共接收: 83000
1000 列成功地大量複製到主機檔案。總共接收: 84000
1000 列成功地大量複製到主機檔案。總共接收: 85000
1000 列成功地大量複製到主機檔案。總共接收: 86000
1000 列成功地大量複製到主機檔案。總共接收: 87000
1000 列成功地大量複製到主機檔案。總共接收: 88000
1000 列成功地大量複製到主機檔案。總共接收: 89000
1000 列成功地大量複製到主機檔案。總共接收: 90000
1000 列成功地大量複製到主機檔案。總共接收: 91000
1000 列成功地大量複製到主機檔案。總共接收: 92000
1000 列成功地大量複製到主機檔案。總共接收: 93000
1000 列成功地大量複製到主機檔案。總共接收: 94000
1000 列成功地大量複製到主機檔案。總共接收: 95000
1000 列成功地大量複製到主機檔案。總共接收: 96000
1000 列成功地大量複製到主機檔案。總共接收: 97000
1000 列成功地大量複製到主機檔案。總共接收: 98000
1000 列成功地大量複製到主機檔案。總共接收: 99000
1000 列成功地大量複製到主機檔案。總共接收: 100000
1000 列成功地大量複製到主機檔案。總共接收: 101000
1000 列成功地大量複製到主機檔案。總共接收: 102000
1000 列成功地大量複製到主機檔案。總共接收: 103000
1000 列成功地大量複製到主機檔案。總共接收: 104000
1000 列成功地大量複製到主機檔案。總共接收: 105000
1000 列成功地大量複製到主機檔案。總共接收: 106000
1000 列成功地大量複製到主機檔案。總共接收: 107000
1000 列成功地大量複製到主機檔案。總共接收: 108000
1000 列成功地大量複製到主機檔案。總共接收: 109000
1000 列成功地大量複製到主機檔案。總共接收: 110000
1000 列成功地大量複製到主機檔案。總共接收: 111000
1000 列成功地大量複製到主機檔案。總共接收: 112000
1000 列成功地大量複製到主機檔案。總共接收: 113000
1000 列成功地大量複製到主機檔案。總共接收: 114000
1000 列成功地大量複製到主機檔案。總共接收: 115000
1000 列成功地大量複製到主機檔案。總共接收: 116000
1000 列成功地大量複製到主機檔案。總共接收: 117000
1000 列成功地大量複製到主機檔案。總共接收: 118000
1000 列成功地大量複製到主機檔案。總共接收: 119000
1000 列成功地大量複製到主機檔案。總共接收: 120000
1000 列成功地大量複製到主機檔案。總共接收: 121000
1000 列成功地大量複製到主機檔案。總共接收: 122000
1000 列成功地大量複製到主機檔案。總共接收: 123000
1000 列成功地大量複製到主機檔案。總共接收: 124000
1000 列成功地大量複製到主機檔案。總共接收: 125000
1000 列成功地大量複製到主機檔案。總共接收: 126000
1000 列成功地大量複製到主機檔案。總共接收: 127000
1000 列成功地大量複製到主機檔案。總共接收: 128000
1000 列成功地大量複製到主機檔案。總共接收: 129000
1000 列成功地大量複製到主機檔案。總共接收: 130000
1000 列成功地大量複製到主機檔案。總共接收: 131000
1000 列成功地大量複製到主機檔案。總共接收: 132000
1000 列成功地大量複製到主機檔案。總共接收: 133000
1000 列成功地大量複製到主機檔案。總共接收: 134000
1000 列成功地大量複製到主機檔案。總共接收: 135000
1000 列成功地大量複製到主機檔案。總共接收: 136000
1000 列成功地大量複製到主機檔案。總共接收: 137000
1000 列成功地大量複製到主機檔案。總共接收: 138000
1000 列成功地大量複製到主機檔案。總共接收: 139000
1000 列成功地大量複製到主機檔案。總共接收: 140000
1000 列成功地大量複製到主機檔案。總共接收: 141000
1000 列成功地大量複製到主機檔案。總共接收: 142000
1000 列成功地大量複製到主機檔案。總共接收: 143000
1000 列成功地大量複製到主機檔案。總共接收: 144000
1000 列成功地大量複製到主機檔案。總共接收: 145000
1000 列成功地大量複製到主機檔案。總共接收: 146000
1000 列成功地大量複製到主機檔案。總共接收: 147000
1000 列成功地大量複製到主機檔案。總共接收: 148000
1000 列成功地大量複製到主機檔案。總共接收: 149000
1000 列成功地大量複製到主機檔案。總共接收: 150000
1000 列成功地大量複製到主機檔案。總共接收: 151000
1000 列成功地大量複製到主機檔案。總共接收: 152000
1000 列成功地大量複製到主機檔案。總共接收: 153000
1000 列成功地大量複製到主機檔案。總共接收: 154000
1000 列成功地大量複製到主機檔案。總共接收: 155000
1000 列成功地大量複製到主機檔案。總共接收: 156000
1000 列成功地大量複製到主機檔案。總共接收: 157000
1000 列成功地大量複製到主機檔案。總共接收: 158000
1000 列成功地大量複製到主機檔案。總共接收: 159000
1000 列成功地大量複製到主機檔案。總共接收: 160000
1000 列成功地大量複製到主機檔案。總共接收: 161000
1000 列成功地大量複製到主機檔案。總共接收: 162000
1000 列成功地大量複製到主機檔案。總共接收: 163000
1000 列成功地大量複製到主機檔案。總共接收: 164000
1000 列成功地大量複製到主機檔案。總共接收: 165000
1000 列成功地大量複製到主機檔案。總共接收: 166000
1000 列成功地大量複製到主機檔案。總共接收: 167000
1000 列成功地大量複製到主機檔案。總共接收: 168000
1000 列成功地大量複製到主機檔案。總共接收: 169000
1000 列成功地大量複製到主機檔案。總共接收: 170000
1000 列成功地大量複製到主機檔案。總共接收: 171000
1000 列成功地大量複製到主機檔案。總共接收: 172000
1000 列成功地大量複製到主機檔案。總共接收: 173000
1000 列成功地大量複製到主機檔案。總共接收: 174000
1000 列成功地大量複製到主機檔案。總共接收: 175000
1000 列成功地大量複製到主機檔案。總共接收: 176000
1000 列成功地大量複製到主機檔案。總共接收: 177000
1000 列成功地大量複製到主機檔案。總共接收: 178000
1000 列成功地大量複製到主機檔案。總共接收: 179000
1000 列成功地大量複製到主機檔案。總共接收: 180000
1000 列成功地大量複製到主機檔案。總共接收: 181000
1000 列成功地大量複製到主機檔案。總共接收: 182000
1000 列成功地大量複製到主機檔案。總共接收: 183000
1000 列成功地大量複製到主機檔案。總共接收: 184000
1000 列成功地大量複製到主機檔案。總共接收: 185000
1000 列成功地大量複製到主機檔案。總共接收: 186000
1000 列成功地大量複製到主機檔案。總共接收: 187000
1000 列成功地大量複製到主機檔案。總共接收: 188000
1000 列成功地大量複製到主機檔案。總共接收: 189000
1000 列成功地大量複製到主機檔案。總共接收: 190000
1000 列成功地大量複製到主機檔案。總共接收: 191000
1000 列成功地大量複製到主機檔案。總共接收: 192000
1000 列成功地大量複製到主機檔案。總共接收: 193000
1000 列成功地大量複製到主機檔案。總共接收: 194000
1000 列成功地大量複製到主機檔案。總共接收: 195000
1000 列成功地大量複製到主機檔案。總共接收: 196000
1000 列成功地大量複製到主機檔案。總共接收: 197000
1000 列成功地大量複製到主機檔案。總共接收: 198000
1000 列成功地大量複製到主機檔案。總共接收: 199000
1000 列成功地大量複製到主機檔案。總共接收: 200000
NULL
已複製 200699 個資料列。
網路封包大小 (位元組): 4096
時間 (毫秒) 總計 : 13781 平均 : (每秒 14563.46 資料列)
NULL(207 個資料列受到影響)組態選項 'show advanced options' 從 1 變更為 1。請執行 RECONFIGURE 陳述式來安裝。
組態選項 'xp_cmdshell' 從 1 變更為 1。請執行 RECONFIGURE 陳述式來安裝。
GO
/****** Object: StoredProcedure [dbo].[H_SqlToExcel] Script Date: 01/22/2009 17:13:47 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROC [dbo].[H_SqlToExcel]
(
@Path varchar(100),--文件存放路径
@Fname varchar(100),--文件名字
@SheetName varchar(80),---工作表名字
@SqlStr varchar(8000)--查询语句,如果查询语句中使用了order by ,请加上top 100 percent,注意,如果导出表/视图,用上面的存储过程
)
AS
SET NOCOUNT ONdeclare @sql varchar(8000)
declare @obj int--OLE对象
declare @constr varchar(8000)
declare @err int
declare @out int
declare @fdlist varchar(8000)
declare @tbname sysname--临时表
declare @Src nvarchar(200)
declare @Desc nvarchar(200)set @tbname='##tmp_'+convert(varchar(38),newid())exec('select * into ['+@tbname +'] from '+'('+@sqlStr+') A')select @fdlist = ''set @sql= @path+@fname
set @constr='DRIVER={Microsoft Excel Driver (*.xls)};DSN='''';READONLY=FALSE'
+';CREATE_DB="'+@sql+'";DBQ='+@sql--生成Excel的列
set @sql = ''
select @sql = @sql+','+'['+a.name+'] '+ case when b.name like '%char' then case when a.length >255 then 'memo' else 'text('+cast(a.length as varchar)+')' end
when b.name like '%int' or b.name='bit' then 'int'
when b.name like '%datetime' then 'datetime'
when b.name like '%money' then 'money'
when b.name like '%text' then 'memo'
else b.name
end,
@fdlist = @fdlist+','+'['+a.name+']'
from tempdb..syscolumns a join tempdb..systypes b on a.xtype = b.xusertype
where b.name not in('image','uniqueidentifier','sql_variant','varbinary','binary','timestamp')
and id in(select id from tempdb..sysobjects where name = @tbname) order by colorder
if @@rowcount=0 returnset @fdlist = substring(@fdlist,2,8000)--连接数据库
exec @err=sp_oacreate 'adodb.connection',@obj out
if @err <> 0 goto lberror
exec @err=sp_oamethod @obj,'open',null,@constr
if @err <> 0 goto lberror
--创建工作薄
select @sql='create table ['+@sheetname
+']('+substring(@sql,2,8000)+')'exec @err=sp_oamethod @obj,'execute',@out out,@sql--@sql为excute方法提供参数if @err <> 0 goto lberrorexec @err=sp_oadestroy @obj--导入数据
set @sql='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES
;DATABASE='+@path+@fname+''',['+@sheetname+'$])'
--print @sql
exec ('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from ['+@tbname+']')exec('drop table ['+@tbname+']')
return
lberror:
exec sp_oageterrorinfo 0,@src out,@desc outlbexit:
select cast(@err as varbinary(4)) as 错误号
,@src as 错误源,@desc as 错误描述
select @sql,@constr,@fdlist