这是一个输出为Excel的功能,供参考。 procedure ExportToExcelFile ; var vE,vWB,vSh : OleVariant ; iColCount,iRow,I : integer ; S : string ; begin try Application.ProcessMessages ; vE := CreateOleObject('Excel.Application') ; except Application.MessageBox('无法输出为Excel文件,请检查Excel是否安装配置正确','启动Excel出错',MB_ICONINFORMATION+MB_OK) ; Exit ; end; try Application.ProcessMessages ; vWB := vE.WorkBooks.Add ; if vWB.Sheets.Count < 1 then vWB.Sheets.Add ; vSH := vWB.Sheets[1] ; except Application.MessageBox('无法输出为Excel文件,请检查Excel是否安装配置正确','启动Excel出错',MB_ICONINFORMATION+MB_OK) ; Exit ; end; //设置列标题 iRow := 1 ; Application.ProcessMessages ; iColCount := FTableColumns.Count ; for I := 0 to iColCount - 1 do begin with PTableColumn(FTableColumns[I])^ do vSH.Cells[iRow,I+1] := Field.DisplayLabel ; end ; //输出数据内容 D.First; while not D.Eof do begin Application.ProcessMessages; if not frmPrintStatus.Visible then Break; frmPrintStatus.ProgressBarStep; Inc(iRow) ; for I := 0 to FTableColumns.Count - 1 do with PTableColumn(FTableColumns[I])^ do begin if Alignment = taLeftJustify then vSH.Cells[iRow,I+1] := '''' + Field.AsString else vSH.Cells[iRow,I+1] := Field.AsString ; end; D.Next; end; if frmPrintStatus.Visible then begin vWB.SaveAs(SaveDlg.FileName) ; end; vWB.Close(False) ; vE.Quit ; end;
procedure ExportToExcelFile ;
var
vE,vWB,vSh : OleVariant ;
iColCount,iRow,I : integer ;
S : string ;
begin
try
Application.ProcessMessages ;
vE := CreateOleObject('Excel.Application') ;
except
Application.MessageBox('无法输出为Excel文件,请检查Excel是否安装配置正确','启动Excel出错',MB_ICONINFORMATION+MB_OK) ;
Exit ;
end;
try
Application.ProcessMessages ;
vWB := vE.WorkBooks.Add ;
if vWB.Sheets.Count < 1 then
vWB.Sheets.Add ;
vSH := vWB.Sheets[1] ;
except
Application.MessageBox('无法输出为Excel文件,请检查Excel是否安装配置正确','启动Excel出错',MB_ICONINFORMATION+MB_OK) ;
Exit ;
end;
//设置列标题
iRow := 1 ;
Application.ProcessMessages ;
iColCount := FTableColumns.Count ;
for I := 0 to iColCount - 1 do
begin
with PTableColumn(FTableColumns[I])^ do
vSH.Cells[iRow,I+1] := Field.DisplayLabel ;
end ;
//输出数据内容
D.First;
while not D.Eof do
begin
Application.ProcessMessages;
if not frmPrintStatus.Visible then Break;
frmPrintStatus.ProgressBarStep;
Inc(iRow) ;
for I := 0 to FTableColumns.Count - 1 do
with PTableColumn(FTableColumns[I])^ do
begin
if Alignment = taLeftJustify then
vSH.Cells[iRow,I+1] := '''' + Field.AsString
else
vSH.Cells[iRow,I+1] := Field.AsString ;
end;
D.Next;
end; if frmPrintStatus.Visible then
begin
vWB.SaveAs(SaveDlg.FileName) ;
end;
vWB.Close(False) ;
vE.Quit ;
end;