How to create a components at run-time?

Hi, 
How to create a component at run-time?
I do it like this:

  TMyForm= class(TForm)
  private
    FDataModule: TDataModule;
    FReport: TFlexCelReport;
    FXLSAdapter: TXLSAdapter;
    ....
  end;

procedure TMyForm.CreateComponents(ADataSet: TDataSet);
begin
  Application.CreateForm(TDataModule, FDataModule);
  FDataModule.Name := 'dmDataModule';

  FXLSAdapter := TXLSAdapter.Create(FDataModule);
  FXLSAdapter.AllowOverwritingFiles := True;

  FReport := TFlexCelReport.Create(FDataModule);
  FReport.Adapter := FXLSAdapter;
  FReport.DataModule := FDataModule;
  FDataModule.InsertComponent(ADataSet);
end;

procedure TMyForm.Print(ATemplateFile, AOutFileName: string);
begin
  if FileExists(ATemplateFile) then
  begin
    FReport.Template := ATemplateFile;
    FReport.FileName := AOutFileName;
    FReport.Run;
  end;
...
end;

This creates a file containing a copy of the template, but contains no data. How to create components in the Run-Time?

Thanks for your help

Hi,


I don't see a problem with your code. FlexCel works fine with everything created at runtime, there is no difference there.
My guess would be more in the wrong template (maybe missing a dataset range?)

I tried your example here, and it worked fine. As I can't attach here, I will paset the full code below. It is jaut a new console app with this code:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  XlsAdapter,
  UFlexCelReport,
  Forms,
  Classes,
  DB,
  DBClient,
  UFlxMemTable;

var
    FDataModule: TDataModule;
    FReport: TFlexCelReport;
    FXLSAdapter: TXLSAdapter;
    ADataSet: TFlxMemTable;

procedure CreateComponents;
var
  f: TFlxDbMemColumn;
begin
  Application.CreateForm(TDataModule, FDataModule);
  FDataModule.Name := 'dmDataModule';

  FXLSAdapter := TXLSAdapter.Create(FDataModule);
  FXLSAdapter.AllowOverwritingFiles := True;

  FReport := TFlexCelReport.Create(FDataModule);
  FReport.Adapter := FXLSAdapter;
  FReport.DataModule := FDataModule;

  ADataSet := TFlxMemTable.Create(FDataModule);
  ADataSet.Name := 'Orders';
  f := ADataSet.Columns.Add as TFlxDbMemColumn;
  f.Name := 'orderno';

  ADataSet.AddRecord([1]);
  ADataSet.AddRecord([2]);
  ADataSet.AddRecord([3]);


  FDataModule.InsertComponent(ADataSet);
end;

begin
  try
    CreateComponents;
    FReport.Template := '....\testtemplate.xls';
    FReport.FileName := '....\result.xls';
    FREport.Run;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


I tried to keep the code as similar as yours, I only defined a dataset (a tflxmemtable) and nothing else.
The xls template is just an empty xls file with:
Cell A1:##orders##orderno
Named range:  orders in cell A1.

RUnning the report returns "1", "2" and "3" as expected.
Do you see anything that's different?
Can you try running this same report from a form to see if there is any difference? Also, can you right click the FlexCelReport in the form and see if "Check template" returns any insight?

Other than that, it should just work, there is nothin in FlexCel that makes it design-only, and in fact I use it with runtime creation in many cases.

Regards,
    Adrian.

Ps: People, please try to give us a couple of hours for our answer; I was writing this in an email when I got the same question in this post and had to copy-paste everything from the mail to this post. If we do not answer after 24 hours, yes please retry because we might have not got the question.  But not before that...

Hello, Adrian. 


Thanks for the help, everything is working.

Regards,
  Roman.