怎样才能快速的把Query中的数据存成Excel文档
编号:QA004709
建立日期: 2003年8月2日 最后修改日期:2003年8月2日
所属类别:
Q
谭必平 (TX):
请问在Delphi中我用query控件查找到几万条纪录并用DBGrid显示,怎样才能快速的把Query中的数据存成Excel文档(如PowerBuilder中的SaveAs一样)?
A回答:
大致有下面几个办法:
1)存成CSV文件,这种文件可以被Excel打开。如:
uses ADOInt;
procedure TForm1.Button1Click(Sender: TObject);
var
Rs : string;
SL: TStringList;
begin
try
SL := TStringList.Create;
Rs := AdoQuery1.Recordset.GetString(adClipString, -1, #59,#13, '');
SL.Text := Rs;
SL.SaveToFile('C:\MyQry.csv');
finally
SL.Free;
end;
end;
2)利用Excel的数据库接口,这种方法需要一定的编程,而且不能控制格式:
with ADODataSet1 do
begin
CommandText := '$'; ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ FileName
+ ';Extended Properties=Excel 8.0;Persist Security Info=False';
Open;
{dO SOME STUFF}
Close;
end;
3)借助Automation接口,这样可以控制输出的格式,但需要编写大量代码。可以参考
QA003964 "如何在DELPHI中控制EXCEL"。
4)使用商业控件,如SMExport component suite(http://www.scalabium.com/)和XLSReadWrite(http://www.axolot.com/components/index_g.htm)。
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, Delphi, VCL, Borland, Office开发, office, ms office, office xp, office 97, 数据库, database, query。
| |
|
|
| |
|
|