Table of Contents

Tables as datasources (Delphi)

Note

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

Overview

On the other examples here, we use __NAMES__ to define where the data has to go in the template. Here, we use tables instead.

Concepts

  • Tables can work like names to define data bands, if they are named starting with "__", "_" and ending with the same, just as named ranges.

  • To have the table expand when data is inserted, the table must have an empty row after the data.

  • Tables behave like "X" ranges, even if you don't name them as "__Table__X". You can name them with an X at the end or not, but no matter what you do, they will behave as if the "X" was in the name, and remove the empty row that you need to have for the tables to expand.

  • Tables will be renamed after the report runs, by removing the "_" or "__" from the start and the end. If you don't want them to be renamed, you can change the property TFlexCelReport.RenameExcelTablesUsedAsBands.

  • A __Table__ is just a shortcut for defining a __Table__X named range. In this example you could have got the same results by having the table named "Products" instead of "__Products__" and defining a name "__Products__X" from A38 to D38.

  • __Tables__ can be embedded in a master detail relationship with other __Names__. It is impossible to have a table inside a table (this is an Excel limitation), but you can have a __Table__ inside a __Name__ as in this example and the table will be copied for every record of __Name__

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;

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

var
  MainForm: TMainForm;

implementation
uses IOUtils, DemoData;

{$R *.dfm}

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(DemoTables);
    Report.SetValue('Date', Now);
    Report.Run(
      TPath.Combine(GetDataPath, 'Tables As Datasources.template.xlsx'),
      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.