excelapplication:启动Excel并建立同excel的连接
excelworkbook设置excel工作簿
excelworksheet设置工作簿中的工作页;
我有一个简单的例子,是导出数据库为excel格式的,希望对你有帮助
var
i,row:integer;
begin
if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass;
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
if not adoQuery1.Active then
begin
adoQuery1.Open;
end;
for i:=0 to adoQuery1.Fields.Count-1 do
ExcelWOrkSheet1.Cells.Item[1,i+1]:=adoQuery1.Fields.FieldName;
row:=2;
while not adoQuery1.Eof do
begin
for i:=0 to adoQuery1.Fields.Count-1 do
begin
ExcelWOrkSheet1.Cells.Item[row,i+1]:=adoQuery1.Fields.AsString;
end;
row:=row+1;
adoQuery1.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWOrkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
Application.MessageBox('Export successfully!','Information',0);
end;
end;