您的位置:首页 > 其它

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

2007-04-03 17:01 573 查看
主 题:将不同类型的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的应该都没有问题了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: