Example before question:
var
oXlsReport: TFlexCelReport;
oQuery: TADQuery;
...
oXlsReport.SetValue('DATUM', Now);
oXlsReport.AddTable('Q', oQuery);
...
oQuery.SQL.Text := 'select ...';
oXlsReport.Run(oTemplate1, oReport1); // Ok
...
oQuery.SQL.Text := 'select ...';
oXlsReport.Run(oTemplate2, oReport2); // EFlexCelCoreException with message 'The named range "__Q__X" on the Excel template refers to DataTable "Q" which is not defined. Verify that you added the dataset with FlexCelReport.AddTable method.'
...
If oXlsReport.AddTable('Q', oQuery) before oXlsReport.Run(oTemplate2, oReport2) everything is ok.
Just out of curiosity - why AddTable again but report variables is not necessary define again ?
TIA and best regards
Branko
It shouldn't work that way. I've looked at the code, and I can't see why the tables would be removed.
I then tried modifying the "Range Reports" demo to run the report twice:
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, 'Range Reports.template.xls'),
SaveDialog.FileName);
Report.Run(
TPath.Combine(GetDataPath, 'Range 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;
And it works correctly. Can you double check that you aren't calling oXlsReport.ClearTables somewhere after running the first report? That's the only reason I can think of for the tables being cleared. Or if you have a way I can reproduce this, so I can take a look.
It it is important:
var
oTemplate1, oReport1: TMemoryStream;
After oXlsReport.Run(oTemplate1, oReport1) DataSourceList is empty.
ShowMessage(IntToStr(oXlsReport.GetDataTables.Count)); // shows 1
oXlsReport.Run(oTemplate1, oReport1);
ShowMessage(IntToStr(oXlsReport.GetDataTables.Count)); // shows 0
Between first and second ShowMessage() is only oXlsReport.Run().
Branko