在很多的时候,我们需要将我们的数据导出到Excel中进行加工,在Access中有现成的工具可以实现 可是在Delphi中却偏偏没有,无论如何我们需要这么一个工具,那么,事不宜迟,细细一想,最好的方法莫过 于直接在程序中按照Excel的格式生成Excel文件,使用Ole技术直接调用Excel实例,由于第一种方法技术实现 过于复杂,好在一般的计算机上已经安装了Office,在此就第二种方法“抛一块砖”。 在此做一个示例以方便说明,我们考虑到导出数据有很多的情况是Master/Detail数据,因此就用两个 TQuery和一个TDataSource,如果有更多的层次,只需要增加TQuery和TDataSource即可,以下使用了两个TQuery ,分别是:qryMaster和qryDetail,一个TDataSource:dsSource; 步骤是: 1.建立一个Excel实例 2.创建一个工作表 3.创建一个TStringList,用来装数据 4.穷举数据表,将数据存入TStringList 5.将TStringList的数据复制到剪贴板 6.把剪贴板中的数据粘贴到Excel 本方法的优点在于:比逐条写入到Excel中的速度大大提高,我曾试过,在P3933,256M的机器上用普通 的方法,导出1000条数据大约需要2分钟,而该用本方法后只需要8秒。function ToExcel():boolean; var y :integer; tsList :TStringList; s :string; aSheet :Variant; begin result:=true; Excel.Connect; // 打开Excel Excel.Visible[0]:=true; // 显示Excel Excel.Workbooks.Add(xlWBATWorksheet,0); aSheet:=excel.Worksheets.Item[1]; tsList:=TStringList.Create; try try with qryMaster do begin Open; First; While Not Eof do begin s:=''; for y:=0 to FieldCount-1 do begin s:=s+Fields[y].AsString+#9; Application.ProcessMessages; end; tsList.Add(s); // 从表 if qryDetail<>nil then begin with qryDetail do begin Open; First; while Not Eof do begin s:=''; for y:=0 to FieldCount-1 do begin s:=s+Fields[y].AsString+#9; Application.ProcessMessages; end; tsList.Add(s); next; end; end; end; next; end; Close; end; Clipboard.AsText:=tsList.Text; except result:=false; end; finally tsList.Free; end; Excel.Disconnect; aSheet.Paste; MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK); end;unit myexcel;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, StdCtrls, Buttons, Grids, DBGrids, Excel97, Word97, OleServer, ExtCtrls, DBCtrls;type TForm1 = class(TForm) ExcelApplication1: TExcelApplication; DataSource1: TDataSource; Query1: TQuery; DBGrid1: TDBGrid; DBImage1: TDBImage; BitBtn1: TBitBtn; Label1: TLabel; DBNavigator1: TDBNavigator; procedure BitBtn1Click(Sender: TObject); private { Private declarations } procedure ToExcel(); public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM} procedure TForm1.ToExcel(); var RangeE:Excel97.Range; Row:Integer; Book1:TBookStr; procedure InsertRecord(); var I:Integer; begin with Query1 do begin //加入字段标题 RangeE:=ExcelApplication1.ActiveCell; for I:=0 to Fields.Count -1 do begin RangeE.Value:=Fields[I].DisplayLabel; RangeE:=RangeE.Next; end; //加入字段数据 DisableControls; try Book1:=Book; try First; Row:=2; while not EOF do begin RangeE:=ExcelApplication1.Range['A'+IntToStr(Row),'A'+IntToStr(Row)]; for I:=0 to Fields.Count -1 do begin RangeE.Value:=Fields[I].AsString; RangeE:=RangeE.Next; end; Next; Inc(Row); end; finally Book1:=Book; end; finally EnableControls; end; end; end; begin try ExcelApplication1.Connect;//打开Excel应用程序 ExcelApplication1.Workbooks.Add(NULL,0); //创建Excel工作簿 InsertRecord();//加入记录到工作簿中 ExcelApplication1.Visible[0]:=True; //使Excel应用程序可见 finally ExcelApplication1.disConnect; //关闭Excel应用程序 end; end;procedure TForm1.BitBtn1Click(Sender: TObject); begin ToExcel(); end;end.
在很多的时候,我们需要将我们的数据导出到Excel中进行加工,在Access中有现成的工具可以实现
可是在Delphi中却偏偏没有,无论如何我们需要这么一个工具,那么,事不宜迟,细细一想,最好的方法莫过
于直接在程序中按照Excel的格式生成Excel文件,使用Ole技术直接调用Excel实例,由于第一种方法技术实现
过于复杂,好在一般的计算机上已经安装了Office,在此就第二种方法“抛一块砖”。
在此做一个示例以方便说明,我们考虑到导出数据有很多的情况是Master/Detail数据,因此就用两个
TQuery和一个TDataSource,如果有更多的层次,只需要增加TQuery和TDataSource即可,以下使用了两个TQuery
,分别是:qryMaster和qryDetail,一个TDataSource:dsSource;
步骤是:
1.建立一个Excel实例
2.创建一个工作表
3.创建一个TStringList,用来装数据
4.穷举数据表,将数据存入TStringList
5.将TStringList的数据复制到剪贴板
6.把剪贴板中的数据粘贴到Excel
本方法的优点在于:比逐条写入到Excel中的速度大大提高,我曾试过,在P3933,256M的机器上用普通
的方法,导出1000条数据大约需要2分钟,而该用本方法后只需要8秒。function ToExcel():boolean;
var
y :integer;
tsList :TStringList;
s :string;
aSheet :Variant;
begin
result:=true;
Excel.Connect; // 打开Excel
Excel.Visible[0]:=true; // 显示Excel
Excel.Workbooks.Add(xlWBATWorksheet,0);
aSheet:=excel.Worksheets.Item[1]; tsList:=TStringList.Create;
try
try
with qryMaster do
begin
Open;
First;
While Not Eof do
begin
s:='';
for y:=0 to FieldCount-1 do
begin
s:=s+Fields[y].AsString+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
// 从表
if qryDetail<>nil then
begin
with qryDetail do
begin
Open;
First;
while Not Eof do
begin
s:='';
for y:=0 to FieldCount-1 do
begin
s:=s+Fields[y].AsString+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
next;
end;
end;
end;
next;
end;
Close;
end;
Clipboard.AsText:=tsList.Text;
except
result:=false;
end;
finally
tsList.Free;
end; Excel.Disconnect;
aSheet.Paste;
MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK);
end;unit myexcel;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Buttons, Grids, DBGrids, Excel97, Word97,
OleServer, ExtCtrls, DBCtrls;type
TForm1 = class(TForm)
ExcelApplication1: TExcelApplication;
DataSource1: TDataSource;
Query1: TQuery;
DBGrid1: TDBGrid;
DBImage1: TDBImage;
BitBtn1: TBitBtn;
Label1: TLabel;
DBNavigator1: TDBNavigator;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
procedure ToExcel();
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
procedure TForm1.ToExcel();
var
RangeE:Excel97.Range;
Row:Integer;
Book1:TBookStr;
procedure InsertRecord();
var
I:Integer;
begin
with Query1 do
begin
//加入字段标题
RangeE:=ExcelApplication1.ActiveCell;
for I:=0 to Fields.Count -1 do
begin
RangeE.Value:=Fields[I].DisplayLabel;
RangeE:=RangeE.Next;
end;
//加入字段数据
DisableControls;
try
Book1:=Book;
try
First;
Row:=2;
while not EOF do
begin
RangeE:=ExcelApplication1.Range['A'+IntToStr(Row),'A'+IntToStr(Row)];
for I:=0 to Fields.Count -1 do
begin
RangeE.Value:=Fields[I].AsString;
RangeE:=RangeE.Next;
end;
Next;
Inc(Row);
end;
finally
Book1:=Book;
end;
finally
EnableControls;
end;
end;
end;
begin
try
ExcelApplication1.Connect;//打开Excel应用程序
ExcelApplication1.Workbooks.Add(NULL,0);
//创建Excel工作簿
InsertRecord();//加入记录到工作簿中
ExcelApplication1.Visible[0]:=True;
//使Excel应用程序可见
finally
ExcelApplication1.disConnect;
//关闭Excel应用程序
end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ToExcel();
end;end.
.dxDBGrid1.SaveToXLS('c:\tt.xls');