电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> 数据库
SQL导出数据到EXCEL文件-数据库专栏,SQL Server
作者:网友供稿 点击:9
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 

create excel xls from t-sql
---------------------------------------------------------------------------
-- create xls script dal - 04/24/2003
--
-- designed for agent scheduling, turn on "append output for step history"
--
-- search for %%% to find adjustable constants and other options
--
-- uses ole for ado and ole db to create the xls file if it does not exist
--   linked server requires the xls to exist before creation
-- uses ole ado to create the xls worksheet for use as a table by t-sql
-- uses linked server to allow t-sql access to xls table
-- uses t-sql to populate te xls worksheet, very fast
--
print begin createxls script at +rtrim(convert(varchar(24),getdate(),121))+
print
go

set nocount on
declare @conn int -- ado connection object to create xls
 , @hr int -- ole return value
 , @src varchar(255) -- ole error source
 , @desc varchar(255) -- ole error description
 , @path varchar(255) -- drive or unc path for xls
 , @connect varchar(255) -- ole db connection string for jet 4 excel isam
 , @wks_created bit -- whether the xls worksheet exists
 , @wks_name varchar(128) -- name of the xls worksheet (table)
 , @servername nvarchar(128) -- linked server name for xls
 , @ddl varchar(8000) -- jet4 ddl for the xls wks table creation
 , @sql varchar(8000) -- insert into xls t-sql
 , @recs int -- number of records added to xls
 , @log bit -- whether to log process detail

-- init variables
select @recs = 0
 -- %%% 1 = verbose output detail, helps find problems, 0 = minimal output detail
 , @log = 1
-- %%% assign the unc or path and name for the xls file, requires read/write access
--   must be accessable from server via sql server service account
--   & sql server agent service account, if scheduled
set @path = c:\temp\test_+convert(varchar(10),getdate(),112)+.xls
-- assign the ado connection string for the xls creation
set @connect = provider=microsoft.jet.oledb.4.0;data source=+@path+;extended properties=excel 8.0
-- %%% assign the linked server name for the xls population
set @servername = excel_test
-- %%% rename table as required, this will also be the xls worksheet name
set @wks_name = people
-- %%% table creation ddl, uses jet4 syntax,
--   text data type = varchar(255) when accessed from t-sql
set @ddl = create table +@wks_name+ (ssn text, name text, phone text)
-- %%% t-sql for table population, note the 4 part naming required by jet4 ole db
--   insert into select, insert into values, and exec sp types are supported
--   linked server does not support select into types
set @sql = insert into +@servername+...+@wks_name+ (ssn, name, phone)
set @sql = @sql+select au_id as ssn
set @sql = @sql+, ltrim(rtrim(isnull(au_fname,)+ +isnull(au_lname,))) as name
set @sql = @sql+, phone as phone
set @sql = @sql+from pubs.dbo.authors

if @log = 1 print created ole adodb.connection object
-- create the conn object
exec @hr = sp_oacreate adodb.connection, @conn out
if @hr <> 0 -- have to use <> as ole / ado can return negative error numbers
begin
 -- return ole error
 exec sp_oageterrorinfo @conn, @src out, @desc out
 select error=convert(varbinary(4),@hr), source=@src, description=@desc
 return
end

if @log = 1 print char(9)+assigned connectionstring property
-- set a the conn objects connectionstring property
--   work-around for error using a variable parameter on the open method
exec @hr = sp_oasetproperty @conn, connectionstring, @connect
if @hr <> 0
begin
 -- return ole error
 exec sp_oageterrorinfo @conn, @src out, @desc out
 select error=convert(varbinary(4),@hr), source=@src, description=@desc
 return
end

if @log = 1 print char(9)+open connection to xls, for file create or append
-- call the open method to create the xls if it does not exist, cant use parameters
exec @hr = sp_oamethod @conn, open
if @hr <> 0
begin
 -- return ole error
 exec sp_oageterrorinfo @conn, @src out, @desc out
 select error=convert(varbinary(4),@hr), source=@src, description=@desc
 return
end

-- %%% this section could be repeated for multiple worksheets (tables)
if @log = 1 print char(9)+execute ddl to create +@wks_name+ worksheet
-- call the execute method to create the work sheet with the @wks_name caption,
--   which is also used as a table reference in t-sql
-- neat way to define column data types in excel worksheet
--   sometimes converting to text is the only work-around for excels general
--   cell formatting, even though the cell contains text, excel tries to format
--   it in a "smart" way, i have even had to use the single quote appended as the
--   1st character in t-sql to force excel to leave it alone
exec @hr = sp_oamethod @conn, execute, null, @ddl, null, 129 -- adcmdtext + adexecutenorecords
-- 0x80040e14 for table exists in ado
if @hr = 0x80040e14
 -- kludge, skip 0x80042732 for ado optional parameters (null) in sql7
 or @hr = 0x80042732
begin
 -- trap these ole errors
 if @hr = 0x80040e14
 begin
  print char(9)++@wks_name+ worksheet exists for append
  set @wks_created = 0
 end
 set @hr = 0 -- ignore these errors
end
if @hr <> 0
begin
 -- return ole error
 exec sp_oageterrorinfo @conn, @src out, @desc out
 select error=convert(varbinary(4),@hr), source=@src, description=@desc
 return
end

if @log = 1 print destroyed ole adodb.connection object
-- destroy the conn object, +++ important to not leak memory +++
exec @hr = sp_oadestroy @conn
if @hr <> 0
begin
 -- return ole error
 exec sp_oageterrorinfo @conn, @src out, @desc out
 select error=convert(varbinary(4),@hr), source=@src, description=@desc
 return
end

-- linked server allows t-sql to access the xls worksheet (table)
--   this must be performed after the ado stuff as the xls must exist
--   and contain the schema for the table, or worksheet
if not exists(select srvname from master.dbo.sysservers where srvname = @servername)
begin
 if @log = 1 print created linked server +@servername+ and login
 exec sp_addlinkedserver @server = @servername
      , @srvproduct = microsoft excel workbook
      , @provider = microsoft.jet.oledb.4.0
      , @datasrc = @path
      , @provstr = excel 8.0
 -- no login name or password are required to connect to the jet4 isam linked server
 exec sp_addlinkedsrvlogin @servername, false
end

-- have to exec the sql, otherwise the sql is evaluated
--   for the linked server before it exists
exec (@sql)
print char(9)+populated +@wks_name+ table with +convert(varchar,@@rowcount)+ rows

-- %%% optional you may leave the linked server for other xls operations
--   remember that the linked server will not create the xls, so remove it
--   when you are done with it, especially if you delete or move the file
if exists(select srvname from master.dbo.sysservers where srvname = @servername)
begin
 if @log = 1 print deleted linked server +@servername+ and login
 exec sp_dropserver @servername, droplogins
end
go

set nocount off
print
print finished createxls script at +rtrim(convert(varchar(24),getdate(),121))+
go

目的
set @path = c:\temp\test_+convert(varchar(10),getdate(),112)+.xls


set @sql = insert into +@servername+...+@wks_name+ (ssn, name, phone)
set @sql = @sql+select au_id as ssn
set @sql = @sql+, ltrim(rtrim(isnull(au_fname,)+ +isnull(au_lname,))) as name
set @sql = @sql+, phone as phone
set @sql = @sql+from pubs.dbo.authors


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·数据库开发个人总结(ADO.NET小结)-.NET教程,数据库应用
·怎么由DataSet将数据导入Excel?-.NET教程,数据库应用
·动态创建SQL Server数据库、表、存储过程-ASP教程,数据库相关
·Win32环境下动态链接库(DLL)编程原理-.NET教程,数据库应用
·封装的ADO.NET对数据库操作经典类-.NET教程,数据库应用
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·DataGrid使用心得(附大量代码)-ASP教程,数据库相关
·用代码创建DataGrid的多链接及checkbox事件响应-.NET教程,数据库应用
·ADO.NET 的最佳实践技巧-.NET教程,数据库应用
·转载: 用纯ASP代码实现图片上传并存入数据库中

最新文章
·根据数据表中数据,生成Powerpoint幻灯片-ASP教程,数据库相关
·DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
·使用用VB处理MYSQL数据库中二进制数据问题-.NET教程,VB.Net语言
·关于DataGridView中如何接收处于编辑状态下的当前信息-ASP教程,数据库相关
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·.net下访问Access数据库需要注意的问题-.NET教程,Asp.Net开发
·ActiveMQ4.1+Spring2.0的POJO JMS方案(上)-.NET教程,数据库应用
·ASP.NET 2.0中直接将Access数据库导入到Excel文件中-.NET教程,Asp.Net开发
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·ASP.NET2.0连接SQL Server数据库详解-.NET教程,Asp.Net开发


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号