将不同类型的ADO数据源导出到Excel(解决方案)

主 题: 将不同类型的ADO数据源导出到Excel(解决方案)
作 者: sas_razor (散步的水)
等 级: 
信 誉 分: 100
人 气: 154
所属社区: C++ Builder 数据库及相关技术
问题点数: 0
回复次数: 0
发表时间: 2006-8-15 10:35:34
   
 
   
这个问题我在网上找了好久,也问了好多人,大家粘出了一些delphi的代码,但试验都没成功.今天从MSDN上看到一篇文章,"How  To  Transfer  Data  from  ADO  Data  Source  to  Excel  with  ADO",出处:http://support.microsoft.com/kb/295646/EN-US/    文章提供了三种方法导出,但经本人试验,只有第三种方法可以在BCB中运用.三种方法如下:  
**复制  
You  can  use  the  SELECT  INTO  statement  to  copy  data  from  any  data  source  that  Jet  can  read  into  any  data  destination,  creating  a  new  table  (or,  in  the  case  of  Excel,  a  new  worksheet)  on  the  fly.  Do  not  use  the  dollar  sign  syntax(不要使用$符标注工作薄),  for  example  [Sheet1$],  when  you  refer  to  a  sheet  name  as  your  destination.  The  destination  workbook  can  exist  or  not  exist;  however,  the  destination  sheet  must  not  yet  exist.  
 
There  are  three  ways  to  write  the  copy  command  that  copies  the  entire  Customers  table  from  the  Microsoft  Access  Northwind  database  into  a  new  sheet  in  an  Excel  workbook.  Each  syntax  requires  a  single  SQL  statement  and  creates  column  headings  in  the  first  row  of  the  destination  worksheet.    
---------------------------------------------------------  
The  following  example  uses  the  SELECT  INTO  syntax:Dim  strSQL  As  String  
strSQL  =  "SELECT  *  INTO  [Excel  8.0;Database="  &  App.Path  &  _    
       "/book1.xls].[Sheet1]  FROM  Customers"  
cnSrc.Execute  strSQL  
------------------------------  
The  following  example  uses  the  SELECT  INTO  ...  IN  syntax:  strSQL  =  "SELECT  *  INTO  [Sheet1]  IN  ''  [Excel  8.0;Database="  &  App.Path  &  _  
       "/book1.xls]  FROM  Customers"  
 
--------------------------------------  
where  the  bracketed  destination  database  information  is  preceded  by  an  empty  pair  of  single  quotes  for  the  type  argument  (the  "Excel  8.0"  portion),  which  is  included  within  the  brackets  when  you  use  this  syntax.    
•  The  following  example  uses  the  alternate  syntax  for  the  IN  clause:  strSQL  =  "SELECT  *  INTO  [Sheet1]  IN  '"  &  App.Path  &  _  
       "/book1.xls'  'Excel  8.0;'  FROM  Customers"  
 
   
------------------------------------------  
 
不知道为什么前两种不行.  
 
**  追加  
How  to  Append  
You  can  use  the  INSERT  INTO  ...  IN  statement  to  append  data  from  any  data  source  that  Jet  can  read  into  any  data  destination.  Both  the  destination  workbook  and  worksheet  must  exist.  Now  that  you  are  referring  to  an  existing  worksheet,  you  must  use  the  standard  dollar  sign  syntax(必须使用$符标注工作薄),  for  example,  [Sheet1$],  when  you  refer  to  a  sheet  name  as  your  destination.  In  addition,  the  column  headings  must  already  be  present;  in  other  words,  this  statement  can  only  be  used  to  append  to  an  existing  table.  
 
There  are  two  ways  to  write  the  append  command  that  copies  the  entire  Customers  table  from  the  Northwind  database  into  an  existing  Excel  worksheet,  which  already  has  the  appropriate  column  headings.  •  The  following  example  uses  the  SELECT  INTO  ...  IN  syntax:  strSQL  =  "INSERT  INTO  [Sheet1$]  IN  ''  [Excel  8.0;Database="  &  App.Path  &  _  
       "/book1.xls]  SELECT  *  FROM  Customers"  
                                                                         
where  the  bracketed  destination  database  information  is  again  preceded  by  an  empty  pair  of  quotes  for  the  type  argument,  which  is  now  included  within  the  brackets.    
•  The  following  example  uses  the  alternate  syntax  for  the  IN  clause:  
strSQL  =  "INSERT  INTO  [Sheet1$]  IN  '"  &  App.Path  &  _  
       "/book1.xls'  'Excel  8.0;'  SELECT  *  FROM  Customers"  
追加也是第二种才行.  
 
总之问题解决了,与大家分享下.  
能导出excel的话,dbf,txt的应该都没有问题了.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值