Several Report Templates

This is my standard report class:

  Report := CreateFlexCel;
  try
    Report.GetInclude := GetIncludes;
    TemplateStream    := Template(ReportName);

    Model.AddSubModel['sagah.aluno'];
    List := Model.DataViewParam['aluno.all', Param].AsCriteria.List<TEntityContatoAluno>;
    Report.AddTable<TEntityContatoAluno>('data', List);

    Report.Run(TemplateStream, AStream);

  finally
    TemplateStream.Free;
    Report.Free;
    List.Free;
  end;


where:
- I get my excel template that is a resource compiled to my exe,
- get a TList<> of the entities that I am going to use for the report
- and execute the report.

This works very well.

But now I got a different situation and my framework is stuck in the concept above.

I have tried the following, that is a change on the code above, but resulting the SAME stream that will be used for preview:

procedure TNaharReportSagahKitMatricula.InternalExecute(AStream: TStream);
var
  Report: TFlexCelReport;
  TemplateStream: TStream;
  DataView: INaharDataView;
  List: TList<TEntityContatoAluno>;
  Aluno: TEntityContatoAluno;
  Data: TList<TEntityContatoAluno>;
  ReportStream: TMemoryStream;


begin
  inherited;

  try
//    Report.GetInclude := GetIncludes;

    Model.AddSubModel['sagah.aluno'];
    List := Model.DataViewParam['aluno.all', Param].AsCriteria.List<TEntityContatoAluno>;

    for Aluno in List do
    begin
      Report := CreateFlexCel;
      TemplateStream    := Template('sagah.cadastroaluno');
      Data := TList<TEntityContatoAluno>.Create;
      Data.Add(Aluno);

      ReportStream  := TMemoryStream.Create;


      Report.AddTable<TEntityContatoAluno>('data', Data);
      Report.Run(TemplateStream, ReportStream);

      AStream.Position := AStream.Size;
      ReportStream.SaveToStream(AStream);
      ReportStream.SaveToFile(Aluno.Nome);

      ReportStream.Free;
      Data.Free;
      TemplateStream.Free;
      Report.Free;
    end;


  finally
    List.Free;
  end;

end;

I am not being carefull on this code, I just want to test the concept.

The thing here is that i need to have that 

      TemplateStream    := Template('sagah.cadastroaluno');

that brings the excel template as a memory stream executed for diferent templates.

AND I need it done for EACH entity on the list.

So i have wondered passing a TList<> with only ONE entity and execute the same data by flexcel with different templates and concatenating the result on the memory stream.

I could have something like

data1:
report1 + report2 + report3
data2:
report1 + report2 + report3

and so on...

IN my framework the resulting memory stream is then attached to the previewer for showing on user screen.

But what I see always is only the first report generated. It does go thru all the reports and concatenate on the stream. But I am not aware of how many reports can be concatenated on the same stream BEFORE going to preview.

Please HELP

Eduardo 

Hi Eduardo,

You can't concatenate Excel files like this: Each excel file is a complete thing on its own: A header,  some data and a footer. If you concatenate the files one after the other you will just get an invalid file.

I am not really sure on what exactly you want to do, but do you want to create a file where each aluno is in its own sheet? If that is it, can't you just make a multisheet report where every sheet is named like <#data.alunoname> or similar?  (take a look at the multiple sheet master detail demo).
Then in the preview you can show all the sheets in one.

You could also do a traditional master/detail report where all alunos go into the same sheet, one after the other. 

Or if you don't want to modify the templates, you could run each template on its own like this:

procedure RunAllReports(const FinalFile: TXlsFile);
var
  xls: TXlsFile;
begin
   xls:= TXlsFile.Create(true);
   try
     xls.Open(templateStream);
     Report.Run(xls);
     FinalFile.InsertAndCopySheets(1, FinalFile.ActiveSheet,  1, xls);
  finally
    xls.Free;
  end;
end;

This will copy all the sheets from every report into a big "FinalFile" TXlsFile object which you can use in the preview. (you can see an example of how to consolidate many files into 1 in the "10.API\80.Consolidating Files" demo)

But as said, I am not really sure I am understanding correctly the question. If I didn't please let me know the idea with more detail on what you expect to generate.

eventually i did not explain clearly.


But as aways you figured it out.

Yes, what I need is to run independently each template, in my case using the same "aluno" for each iteraction.

Since this templates are used independtly and are contracts with page numbers etc, I dont want to change it in any ways, I just what to have a way to select a bunch of templates, like a "template combo" and run this combo for each aluno. 

However I want to view it as a long and unique preview, before sending to the printer.

I believe you gave me the direction.

Just a quick question, this "template combo" I have some templates that needs to print in both sides of the paper, and some are normal one sided printed. 

How can I set this kind of information for each template result? and if I add all in this same preview at moment I select to print everything is it going to preserve the information? 

Many thanks

Eduardo

In order to have different printer settings for different parts, you need different sheets. (As Excel doesn't allow to have different pages in the same sheet with different printer settings).


Then you can print all the sheets in the same long document by selecting to "print all sheets" (In the Custom preview demo, look at the "All sheets" checkbox at the left)

Now, this would apply to printer settings like landscape/portrait or different page sizes. I don't think it will apply to printing duplex or not, because this isn't an Excel setting but a printer setting (and not all printers support duplex printing).

To change this in a page by page basis you might need to use the BeforeNewPage event of FlexCelPrintDocument and change the Duplex setting of the printer depending on the page.