当前位置: 代码迷 >> Sql Server >> 将表头导入到excel中,该怎么解决
  详细解决方案

将表头导入到excel中,该怎么解决

热度:279   发布时间:2016-04-27 21:51:44.0
将表头导入到excel中
我通过以下命令将数据导如到excel  
exec   master..xp_cmdshell   'bcp   "select   *   from   biao   where   year= ' '2006 ' ' "   queryout   c:\Temp.xls     -c   -q   -S "ServerName "   -U "sa "   -P "xxx " '
导入到excel中的数据不包括表头
请问如何将表头也导入到excel

这个问题有点急
希望大家能帮个忙

------解决方案--------------------
bcp就是不带字段名的,在前台做字段名的插入工作,或者利用视图拼接好,注意视图的字段都要转为字符型
------解决方案--------------------
导入导出 BCP导出表头的过程
CREATE proc out2xls
@服务器名 varchar(255),
@库名 varchar(255),
@表名 varchar(255),
@用户名 varchar(100),
@密码 varchar(100),
@路径及文件名 varchar(255)
as
declare @temp1 nvarchar(4000),@temp2 varchar(8000)

set @temp1= 'select @value1= ' ' ' ',@value2= ' ' ' ' select @[email protected]+ ' ', ' ' ' ' ' '+a.name+ ' ' ' '+char(39)+ ' ' [ ' '+a.name+ ' '] ' ',@[email protected]+ ' ',cast( ' '+ ' '[ ' '+a.name+ ' '] ' '+ ' ' as varchar(200)) ' ' from '[email protected]+ '..syscolumns a, '[email protected]+ '..sysobjects d where a.id=d.id and d.name= ' ' '[email protected]+ ' ' ' '+ ' order by a.colorder '


exec sp_executesql @temp1,N '@value1 nvarchar(4000) output , @value2 varchar(8000) output ',@temp1 output,@temp2 output

select @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)

exec( 'select * into '[email protected]+ '.dbo.中间表 from (select '+ @temp1+ ' union all SELECT '[email protected]+ ' FROM '[email protected]+ '.. '[email protected]+ ') tem3 ')


set @temp2= 'bcp '[email protected]+ '.dbo.中间表 out '[email protected]+ ' -c -S '[email protected]+ ' -U '[email protected]+ ' -P '[email protected]

EXEC master..xp_cmdshell @temp2
exec( 'drop table '[email protected]+ '.dbo.中间表 ')
GO
exec out2xls 'daliserver ', 'pubs ', 'jobs ', 'sa ', 'element ', 'c:\a.txt '
go
drop proc out2xls
  相关解决方案