文章主要描述的是SQL Server 带列名导出到excel的实际操过程,以及在其实际操作中的值得我们大家注意的事项与其实际应用代码的描述,以下就是文章的主要内容的详细描述,望大家在浏览之后会对其有更深的了解。
sql语句就用下面的存储过程
数据SQL Server 带列名导出Excel
导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件
,假如文件不存在,将自动创建文件
,假如表不存在,将自动创建表
基于通用性考虑,仅支持SQL Server 带列名导出标准数据类型
邹建 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,'.','#')
SQL Server 带列名导出至excel,检查文件是否已经存在
if right(@path,1)<>'' set @path=@path+'' 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='DRIVER={Microsoft Excel Driver (*.xls)};DSN='''';READON *** =FALSE' +';CREATE_DB="'+@sql+'";DBQ='+@sql else set @constr='Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 5.0;HDR=YES' +';DATABASE='+@sql+'"'
连接数据库
exec @err=sp_oacreate 'adodb.connection',@obj out if @err<>0 goto lberr exec @err=sp_oamethod @obj,'open',null,@constr if @err<>0 goto lberr
创建表的SQL
declare @tbname sysname set @tbname='##tmp_'+convert(varchar(38),newid()) set @sql='select * into ['+@tbname+'] from('+@sqlstr+') a' exec(@sql) select @sql='',@fdlist='' select @fdlist=@fdlist+','+a.name ,@sql=@sql+',['+a.name+'] ' +case when b.name in('char','nchar','varchar','nvarchar') then 'text('+cast(case when a.length>255 then 255 else a.length end as varchar)+')' when b.name in('tynyint','int','bigint','tinyint') then 'int' when b.name in(' *** alldatetime','datetime') then 'datetime' when b.name in('money',' *** allmoney') then 'money' else b.name end FROM tempdb..syscolumns a left join tempdb..systypes b on a.xtype=b.xusertype where b.name not in('image','text','uniqueidentifier','sql_variant','ntext','varbinary','binary','timestamp') and a.id=(select id from tempdb..sysobjects where name=@tbname) select @sql='create table ['+@sheetname +']('+substring(@sql,2,8000)+')' ,@fdlist=substring(@fdlist,2,8000) exec @err=sp_oamethod @obj,'execute',@out out,@sql if @err<>0 goto lberr exec @err=sp_oadestroy @obj
导入数据
set @sql='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 5.0;HDR=YES ;DATABASE='+@path+@fname+''',['+@sheetname+' $])' exec('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from ['+@tbname+']') set @sql='drop table ['+@tbname+']' 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
以上的相关内容就是对
SQL Server 带列名导出至excel的介绍,望你能有所收获。
国外媒体CNET NEWS就这个“上帝模式”和微软Windows部门主管Steven Sinofsky进行邮件交流后得知,这其实只是众多快捷方式中的一个,Sinofsky在邮件里给出了十多个这类快捷方式的关键字符串。 它们的建立方法和之前的“上帝模式”一样,在任意位置新建的一个文件夹,然后改名即可...
北京时间1月13日消息,根据计划,微软将于2015年1月13日正式结束对Windows 7SP1的“主流支持”,而这也标志着“扩展支持”阶段的开始,这个阶段将于2020年1月14日结束。 2013年10月份,微软终止销售独立的Windows 7系统安装包,并要求制造商于2014年10月份停止生...
Intel会在2015年推出Broadwell、Skylake两代14nm处理器,前者可以使用9系芯片组,Skylake升级到了LGA1151插槽,需要搭配新的100系列芯片组,但初期的Skylake-S又不能超倍频,所以今年Q2季度开始会很混杂。Skylake一代会支持DDR4,100系列芯片组升...
当我在虚拟机的linux系统中ping本机的ip发现ping不通,而本机可以ping通虚拟机中的ip。应该是出于安全考虑吧,Windows 7默认在防火墙里禁止了。 1、查看主机ip 打开“开始”程序中的“附件”,找到“命令提示符”打开。输入命令:ipconfig,可以看到本机的ip是192.1...
1月13日消息,微软今天为Windows 7(SP1)用户重新推送了编号为KB2952664的更新补丁,该更新在官方KB知识库中的描述为“Windows 7系统升级兼容性更新”,用于帮助微软改进当前版本系统,提升升级到最新版Win10系统的体验。 这项更新还用于在系统中显示Windows10更新提...
很多朋友从XP升级迈入了Windows 7的殿堂,却发现,Windows 7确实好用和华丽,可是有一些地方不太适应。今天举出一则例子,是把Windows 7系统下的宋体替换为XP系统下的字体。这个并非无聊,大家去网上搜索下,就知道有多少人想要这么做了。 没办法,每人的需求总是那么的不同,好吧,怀旧...