sql server 带列名导出至excel

作者:佚名 来源:sudu.cn 时间:2008-11-25 11:07:00 


--sql语句就用下面的存储过程
/*--数据导出Excel

导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件
,如果文件不存在,将自动创建文件
,如果表不存在,将自动创建表
基于通用性考虑,仅支持导出标准数据类型
--邹建 2003.10--*/

/*--调用示例

p_exporttb @sqlstr=’select * from 地区资料’
,@path=’c:\’,@fname=’aa.xls’,@sheetname=’地区资料’
--*/
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[p_exporttb]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [dbo].[p_exporttb]
GO

create proc p_exporttb
@sqlstr sysname, --查询语句,如果查询语句中使用了order by ,请加上top 100 percent
@path nvarchar(1000), --文件存放目录
@fname nvarchar(250), --文件名
@sheetname varchar(250)=’’ --要创建的工作表名,默认为文件名
as
declare @err int,@src nvarchar(255),@desc nvarchar(255),@out int
declare @obj int,@constr nvarchar(1000),@sql varchar(8000),@fdlist varchar(8000)

--参数检测
if isnull(@fname,’’)=’’ set @fname=’temp.xls’
if isnull(@sheetname,’’)=’’ set @sheetname=replace(@fname,’.’,’#’)

--检查文件是否已存在
if right(@path,1)<>&rsquo;\&rsquo; set @path=@path+&rsquo;\&rsquo;
create table #tb(a bit,b bit,c bit)
set @sql=@path+@fname
insert into #tb exec master..xp_fileexist @sql

--数据库创建语句
set @sql=@path+@fname
if exists(select 1 from #tb where a=1)
set @constr=&rsquo;DRIVER={Microsoft Excel Driver (*.xls)};DSN=&rsquo;&rsquo;&rsquo;&rsquo;;READONLY=FALSE&rsquo;
    +&rsquo;;CREATE_DB="&rsquo;+@sql+&rsquo;";DBQ=&rsquo;+@sql
else
set @constr=&rsquo;Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 5.0;HDR=YES&rsquo;
+&rsquo;;DATABASE=&rsquo;+@sql+&rsquo;"&rsquo;

--连接数据库
exec @err=sp_oacreate &rsquo;adodb.connection&rsquo;,@obj out
if @err<>0 goto lberr

exec @err=sp_oamethod @obj,&rsquo;open&rsquo;,null,@constr
if @err<>0 goto lberr

--创建表的SQL
declare @tbname sysname
set @tbname=&rsquo;##tmp_&rsquo;+convert(varchar(38),newid())
set @sql=&rsquo;select * into [&rsquo;+@tbname+&rsquo;] from(&rsquo;+@sqlstr+&rsquo;) a&rsquo;
exec(@sql)

select @sql=&rsquo;&rsquo;,@fdlist=&rsquo;&rsquo;
select @fdlist=@fdlist+&rsquo;,&rsquo;+a.name
,@sql=@sql+&rsquo;,[&rsquo;+a.name+&rsquo;] &rsquo;
+case when b.name in(&rsquo;char&rsquo;,&rsquo;nchar&rsquo;,&rsquo;varchar&rsquo;,&rsquo;nvarchar&rsquo;) then
&rsquo;text(&rsquo;+cast(case when a.length>255 then 255 else a.length end as varchar)+&rsquo;)&rsquo;
when b.name in(&rsquo;tynyint&rsquo;,&rsquo;int&rsquo;,&rsquo;bigint&rsquo;,&rsquo;tinyint&rsquo;) then &rsquo;int&rsquo;
when b.name in(&rsquo;smalldatetime&rsquo;,&rsquo;datetime&rsquo;) then &rsquo;datetime&rsquo;
when b.name in(&rsquo;money&rsquo;,&rsquo;smallmoney&rsquo;) then &rsquo;money&rsquo;
else b.name end
FROM tempdb..syscolumns a left join tempdb..systypes b on a.xtype=b.xusertype
where b.name not in(&rsquo;image&rsquo;,&rsquo;text&rsquo;,&rsquo;uniqueidentifier&rsquo;,&rsquo;sql_variant&rsquo;,&rsquo;ntext&rsquo;,&rsquo;varbinary&rsquo;,&rsquo;binary&rsquo;,&rsquo;timestamp&rsquo;)
and a.id=(select id from tempdb..sysobjects where name=@tbname)
select @sql=&rsquo;create table [&rsquo;+@sheetname
+&rsquo;](&rsquo;+substring(@sql,2,8000)+&rsquo;)&rsquo;
,@fdlist=substring(@fdlist,2,8000)

exec @err=sp_oamethod @obj,&rsquo;execute&rsquo;,@out out,@sql
if @err<>0 goto lberr

exec @err=sp_oadestroy @obj

--导入数据
set @sql=&rsquo;openrowset(&rsquo;&rsquo;MICROSOFT.JET.OLEDB.4.0&rsquo;&rsquo;,&rsquo;&rsquo;Excel 5.0;HDR=YES
;DATABASE=&rsquo;+@path+@fname+&rsquo;&rsquo;&rsquo;,[&rsquo;+@sheetname+&rsquo;$])&rsquo;

exec(&rsquo;insert into &rsquo;+@sql+&rsquo;(&rsquo;+@fdlist+&rsquo;) select &rsquo;+@fdlist+&rsquo; from [&rsquo;+@tbname+&rsquo;]&rsquo;)

set @sql=&rsquo;drop table [&rsquo;+@tbname+&rsquo;]&rsquo;
exec(@sql)
return

lberr:
exec sp_oageterrorinfo 0,@src out,@desc out
lbexit:
select cast(@err as varbinary(4)) as 错误号
,@src as 错误源,@desc as 错误描述
select @sql,@constr,@fdlist

标签:
0
投稿

猜你喜欢

  • 历数Firefox2.0对XML处理的改进

    2007-11-27 12:41:00
  • SQL Server 2005 FOR XML嵌套查询使用详解

    2009-01-06 11:20:00
  • 如何用表单的方式推送请求的信息?

    2010-06-16 09:47:00
  • PL/SQL实现Oracle数据库任务调度

    2010-07-20 12:57:00
  • 初学ASP编程易犯的一个错误要注意

    2008-11-07 15:08:00
  • 修改、删除数据记录(DELETE\\UPDATE)

    2009-02-27 15:50:00
  • 轻松掌握怎样从Windows命令行启动MySQL

    2009-02-23 17:18:00
  • 10点优化sql数据库技巧

    2008-06-09 15:00:00
  • Sql Server 无日志文件附加

    2010-05-30 11:23:00
  • 详解CSS的优先权

    2008-05-11 18:57:00
  • ASP 使用Filter函数来检索数组

    2011-04-30 16:49:00
  • 彻底弄懂CSS盒子模式之三(浮动的表演和清除的自述)

    2007-05-11 16:52:00
  • 教你四种方法用来查看mysql版本

    2009-06-28 11:13:00
  • Oracle 数组的学习 小知识也要积累,养成好的学习态度

    2009-08-04 12:42:00
  • 英文版面设计的8个禁忌

    2009-10-14 20:42:00
  • 当设计师遭遇HTML5

    2011-08-05 18:59:53
  • MySQL手动安装方法与中文解决方案

    2011-04-25 18:21:00
  • 影响ORACLE汉字显示的字符集问题

    2008-06-13 16:49:00
  • 100%全屏布局设计

    2009-05-15 12:24:00
  • 日文片假名导致 Access 搜索“内存溢出”

    2009-07-07 22:23:00
  • asp之家 网络编程 m.aspxhome.com