unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Menus, Grids, DBGrids, ExtCtrls,comobj, Gauges, ComCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
DBGrid1: TDBGrid;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
DataSource1: TDataSource;
Query1: TQuery;
SaveDialog1: TSaveDialog;
ProgressBar1: TProgressBar;
Gauge1: TGauge;
procedure N2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
const
charArray:array[1..26] of char=
('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
{$R *.DFM}
procedure TForm1.N2Click(Sender: TObject);
var
ss:string;
exc:variant;
excworkbook,excworksheet:variant;
i,j:integer;
str:tstrings;
begin
ss:='temp2';
with query1 do
begin
close;
sql.clear;
sql.add('select * from "'+ss+'"');
open;
end;
try
exc:=CreateOleObject('Excel.Application');
excworkbook:=exc.WorkBooks.Add;
excworksheet:=exc.worksheets.add;
except
showmessage('no install excel');
exit;
end;
str:=tstringlist.Create; //?才﹃??
query1.GetFieldNames(str);
for j:=1 to str.Count do
begin
excworksheet.range[chararray[j]+'1'].value:=str.Strings[j-1] ; //?excel??い糶??琿
end;
query1.First;
//_______progressbar and gauge
progressbar1.Visible:=true;
gauge1.Visible:=true;
progressbar1.Position:=0;
gauge1.Progress:=0;
progressbar1.Max:=query1.RecordCount;
gauge1.MaxValue:=query1.RecordCount;
//_______progressbar and gauge initlization;
i:=1;
while not query1.Eof do
begin
inc(i);
try
for j:=1 to str.Count do
begin
excworksheet.range[chararray[j]+inttostr(i)].value:=query1.Fields[j-1].AsString;
end;
except
showmessage('cann''t export excel');
end;
query1.Next;
//________
progressbar1.StepIt;
gauge1.AddProgress(1);
end;
savedialog1.Filter:='excel(*.xls)|*.xls';
if savedialog1.Execute then
begin
try
excworksheet.saveas(savedialog1.FileName);
except
showmessage('save error');
exit;
end;
end;
exc.quit;
//___________
progressbar1.Visible:=false;
gauge1.Visible:=false;
end;
end.