Use this approach for on-the-fly export if you know the row count at the beginning of the export. See code comments for more details.
Download the FREE package with a full delphi project with this code: go to download page.
PAS Code:unit UnitOTFE; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OExport, OExport_Vcl, OExport_VclForms; type TFormOTFExport = class(TForm) BtnOnTheFlyExport: TButton; LblInfo: TLabel; procedure BtnOnTheFlyExportClick(Sender: TObject); public procedure RunOnTheFlyExport;//CALL THIS PROCEDURE TO EXECUTE THE EXPORT procedure OnTheFlyExport_AddRow(aExport: TOExport; aWorkSheet: TExportWorkSheet; aRow: TExportRow); end; //JUST AND EXAMPLE -> YOU WILL PROBABLY USE A DATABASE QUERY HERE TMyCustomDBQuery = class(TObject) protected fRowCount: Integer; fColCount: Integer; fCurrentRow: Integer; public function GetCell(const aColumnIndex: Integer): String; virtual; abstract; function GetColHeader(const aColumnIndex: Integer): String; function FetchNextRow: Boolean; property RowCount: Integer read fRowCount; property ColCount: Integer read fColCount; public constructor Create; end; TDBQueryMultiplication = class(TMyCustomDBQuery) public function GetCell(const aColumnIndex: Integer): String; override; end; TDBQueryAddition = class(TMyCustomDBQuery) public function GetCell(const aColumnIndex: Integer): String; override; end; var FormOTFExport: TFormOTFExport; implementation uses FormatSQL; {$R *.dfm} procedure TFormOTFExport.BtnOnTheFlyExportClick(Sender: TObject); begin RunOnTheFlyExport; end; procedure TFormOTFExport.OnTheFlyExport_AddRow(aExport: TOExport; aWorkSheet: TExportWorkSheet; aRow: TExportRow); var xQuery: TMyCustomDBQuery; I: Integer; begin xQuery := TMyCustomDBQuery(aWorkSheet.OnTheFlyParam); if aRow.IndexInList = 0 then begin //HEADER ROW aRow.AddCellEmpty.SetBGColor(clNavy).SetFontColor(clWhite); for I := 0 to xQuery.ColCount-1 do aRow.AddCellString(xQuery.GetColHeader(I)).SetBGColor(clNavy). SetFontColor(clWhite); end else begin //GO TO NEXT DB ROW if not xQuery.FetchNextRow then raise Exception.Create('Error'); //COL HEADER aRow.AddCellString(IntToStr(aRow.IndexInList)).SetBGColor(clNavy). SetFontColor(clWhite); //DUMP CELLS for I := 0 to xQuery.ColCount-1 do aRow.AddCellNumber(SqlStrToInt(xQuery.GetCell(I)), 0); end; end; procedure TFormOTFExport.RunOnTheFlyExport; var xExport: TOExport; xQueryMultiplication: TDBQueryMultiplication; xQueryAddition: TDBQueryAddition; begin xExport := TOExport.Create; //JUST AN EXAMPLE -> YOU WILL PROBABLY USE A DATABASE QUERY HERE xQueryMultiplication := TDBQueryMultiplication.Create; xQueryAddition := TDBQueryAddition.Create; try //WE WANT TO USE SAVE-ON-THE-FLY xExport.SaveOnTheFly := True; //NOW DEFINE WORKSHEETS THAT WILL BE EXPORTED with xExport.AddWorkSheet('Multiplication') do begin //DEFINE ROW METHOD FOR THIS WORKSHEET SaveOnTheFlyOnAddRow := OnTheFlyExport_AddRow; //DEFINE ROW COUNT FOR THIS WORKSHEET SaveOnTheFlyRows := xQueryMultiplication.RowCount + 1;//FIRST IS HEADER ROW OnTheFlyParam := xQueryMultiplication; end; with xExport.AddWorkSheet('Addition') do begin //DEFINE ROW METHOD FOR THIS WORKSHEET SaveOnTheFlyOnAddRow := OnTheFlyExport_AddRow; //DEFINE ROW COUNT FOR THIS WORKSHEET SaveOnTheFlyRows := xQueryAddition.RowCount + 1;//FIRST IS HEADER ROW OnTheFlyParam := xQueryAddition; end; //NOW GENERATE THE DOCUMENT xExport.SaveToFileWithDialog('on-the-fly-export', '', True); finally xExport.Free; xQueryMultiplication.Free; xQueryAddition.Free; end; end; { TMyCustomDBQuery } constructor TMyCustomDBQuery.Create; begin fRowCount := 50; fColCount := 50; fCurrentRow := -1; end; function TMyCustomDBQuery.FetchNextRow: Boolean; begin Inc(fCurrentRow); Result := fCurrentRow < fRowCount; end; function TMyCustomDBQuery.GetColHeader(const aColumnIndex: Integer): String; begin Result := IntToStr(aColumnIndex+1) end; { TDBQueryMultiplication } function TDBQueryMultiplication.GetCell(const aColumnIndex: Integer): String; begin Result := IntToStr((aColumnIndex+1)*(fCurrentRow+1)); end; { TDBQueryAddition } function TDBQueryAddition.GetCell(const aColumnIndex: Integer): String; begin Result := IntToStr((aColumnIndex+1)+(fCurrentRow+1)); end; end.DFM Code:
object FormOTFExport: TFormOTFExport Left = 0 Top = 0 Caption = 'FormOTFExport' ClientHeight = 348 ClientWidth = 643 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object LblInfo: TLabel Left = 56 Top = 32 Width = 357 Height = 13 Caption = 'Use this method if you know the row count at the beginning of th' + 'e export.' end object BtnOnTheFlyExport: TButton Left = 48 Top = 72 Width = 145 Height = 25 Caption = 'On-The-Fly Export' TabOrder = 0 OnClick = BtnOnTheFlyExportClick end end