Generic reports (Delphi)
Overview
When you are reporting from an unknown database (for example if you are
writing a DbExport app) you can't use a fixed template, because you
don't know what the columns will be. While FlexCelReport is designed to
handle fixed-form templates, there is some support also for generic
reports. This example together with the Meta Templates
and Generic Reports 2 examples show what
can be done. For more advanced generic reports, you should probably use
the API directly.
Concepts
- You can use <#DataTable.*> to write a full row of data from
a datatable. Cells at the right of the cell containing the
<#DataTable.*> tag will be overwritten. 
- You can use <#DataTable.**> to write a full row with the
column captions on a datatable. Cells at the right of the cell
containing the <#DataTable.**> tag will be overwritten. 
- <DataTable.**> can be placed anywhere, it doesn't need to
be inside a named range. <DataTable.*> should be inside a
named range populating DataTable. 
- Dates: As dates on Excel are just numbers formatted as date,
<#DataTable.*> will format the cell when the column has a
DateTime value. If the time part of the datetime is 0, time will
not be entered. 
- You can use expressions in cells with <#DataTable.*> and
<#DataTable.**> tags. You can also get the current value
of the tag in the cell by writing <#DataTable.*> or
<#DataTable.**> again in the same cell. For example, the
tags: -   <#Table.**><#Column Width(autofit;110)>
 - Will autofit all the columns in the range expanded by the
<#Table.**> tag. While the tags: -   <#Table.*><#if(<#evaluate(mod(column(),2)=0)>;<#format cell(light)>;)>
  <#if(<#table.*>="BONAP";<#format cell(blue)>;)>
  <#if(<#table.**>="OrderDate";<#format cell(longdate)>;)>
 - will perform 3 things: On the first, it will paint alternating columns
with the "light" format. On the second, it will it will highlight
cells that contain the string "BONAP" in blue. On the third, it will
format the cells in the "OrderDate" column to have a longer date
format. 
- Autofilters will be automatically expanded with the
<#DataTable.**> tag. You only need to set them in the cell
with the tag. 
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,
  Grids, DBGrids, ComCtrls, ToolWin, ActnList, ImgList;
type
  TMainForm = class(TForm)
    SaveDialog: TSaveDialog;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    DBGrid1: TDBGrid;
    ADOConnection: TADOConnection;
    Table: TADODataSet;
    Actions: TActionList;
    ActionQuery: TAction;
    ActionExportToExcel: TAction;
    ActionClose: TAction;
    DsTable: TDataSource;
    ToolbarImages: TImageList;
    ToolbarImages_100Scale: TImageList;
    ToolbarImages_300Scale: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure ActionCloseExecute(Sender: TObject);
    procedure ActionQueryExecute(Sender: TObject);
    procedure ActionExportToExcelExecute(Sender: TObject);
  private
    procedure RunReport;
    function GetDataPath: string;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  MainForm: TMainForm;
implementation
uses IOUtils, USQLDialog, UFlexCelHDPI;
{$R *.dfm}
procedure TMainForm.ActionCloseExecute(Sender: TObject);
begin
  Close;
end;
procedure TMainForm.ActionExportToExcelExecute(Sender: TObject);
begin
  RunReport;
end;
procedure TMainForm.ActionQueryExecute(Sender: TObject);
begin
  if EnterSQL.ShowModal <> mrOk then exit;
  Table.Active := false;
  Table.CommandText := EnterSQL.SQL;
  Table.Active := true;
end;
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, []);
  Table.CommandText := 'select * from orders';
  Table.Active := true;
  RegisterForHDPI(Self, nil);
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('Table', Table);
    Report.SetValue('Date', Now);
    Report.SetValue('ReportCaption', Table.CommandText);
    Report.Run(
      TPath.Combine(GetDataPath, 'Generic Reports.template.xls'),
      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.
USQLDialog.pas
unit USQLDialog;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls;
type
  TEnterSQL = class(TForm)
    edSQL: TMemo;
    Label1: TLabel;
    btnCancel: TButton;
    btnOk: TButton;
  private
    { Private declarations }
  public
    function SQL: string;
    { Public declarations }
  end;
var
  EnterSQL: TEnterSQL;
implementation
{$R *.dfm}
{ TEnterSQL }
function TEnterSQL.SQL: string;
begin
  Result := edSQL.Text;
end;
end.