Table of Contents

Fixed footer (Delphi)

Note

This demo is available in your FlexCel installation at <FlexCel Install Folder>\Demo\Delphi\Modules\20.Reports\94a.Fixed Footer and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​VCL-​demos/​tree/​master/​Delphi/​Modules/​20.​Reports/​94a.​Fixed Footer

Overview

When doing simple headers and footers, like for example printing "Page n of m" in every page, then Excel's built-in headers and footers should be enough. But sometimes you need more complex headers or footers, and you need to define them in the cells.

For headers, Excel provides a "Rows to repeat at top" and "Columns to repeat at left" command that allow you to repeat a range of cells in every printed page. But to do footers, Excel doesn't provide a similar "Rows to repeat at bottom" or "Columns to repeat at right". If you need complex footers, then the only option is to manually split the dataset in groups of n rows, and manually copy the footer every time.

Concepts

  • How to use the SPLIT tag (look at Splitting Tables in the Reports designer's guide) to create a report with 40 records per page.

  • How to use the "multiple of" parameter in ATLEAST (look at Ensuring a table has at least N records in the Reports designer's guide) to ensure the dataset has a number of records multiple of 40. This makes sure that the footer will be at the bottom of the last page, not after the last record.

Files

UMainForm.pas

unit UMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  FlexCel.VCLSupport, FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Report, FlexCel.Render,
  {$if CompilerVersion >= 23.0} System.UITypes, {$IFEND}
  ShellApi,
  Controls, Forms, Dialogs, StdCtrls, ExtCtrls, DB, ADODB;

type
  TMainForm = class(TForm)
    btnCancel: TButton;
    btnGo: TButton;
    SaveDialog: TSaveDialog;
    Label1: TLabel;
    ADOConnection: TADOConnection;
    Customers: TADODataSet;
    Orders: TADODataSet;
    procedure btnCancelClick(Sender: TObject);
    procedure btnGoClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure RunReport;
    function GetDataPath: string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation
uses IOUtils;

{$R *.dfm}

function DBFile: string;
begin
  Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..\..\SharedData\Northwind.mdb');
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  ADOConnection.ConnectionString := StringReplace(ADOConnection.ConnectionString, 'Northwind.mdb', DbFile, []);
end;

procedure TMainForm.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.btnGoClick(Sender: TObject);
begin
  RunReport;

end;

function TMainForm.GetDataPath: string;
begin
  Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..');
end;


procedure TMainForm.RunReport;
var
  Report: TFlexCelReport;
begin
  if not SaveDialog.Execute then exit;

  Report := TFlexCelReport.Create(true);
  try
    Report.AddTable('Orders', Orders);
    Report.AddTable('Customers', Customers);
    Report.Run(
      TPath.Combine(GetDataPath, 'Fixed Footer.template' + TPath.GetExtension(SaveDialog.FileName)),
      SaveDialog.FileName);
  finally
    Report.Free;
  end;

  if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL);
  end;


end;

end.