Microsoft office and OpenDocument as a Report generator

As we think that it would be great if we could generate word and excel documents straight from model driven data – we made it happen.

This article explains how you use it

Create a Word template:

image

You can have grids in grids to create structure:

image

Save this as an open document file (odt) , save it to a place where you can access it from an url (maybe you use sharepoint, or just stick it on some website)

Now you need data. Declare a ViewModel in Modlr:

image

(By now you know that a ViewModel transform your model for a specific need – in this case the open document report)

image

 

Create two extra ViewModelColumns in your ViewModel; TemplateUrl and ReportFileName:

image

Make the TemplateUrl column return the Url to where your template from above can be found – maybe in some sharepoint instance as in this example.
Make the ReportFileName column return what the file should be called when produced.

And you are done.

Execute the report by using new EAL operator:

vSomePumpRev.opendocumentreportshow(‘PumpRevDeepReport’) – this will call the OnOpenDocument event on the new IOpenDocumentService – this is because each UI platform has its own way to actually show things to a user.

namespace Eco.Services
{
  // Summary:
  //     OpenDocumentService , ViewModel needs root level column describing url to
  //     template. Column must be namned "TemplateUrl" and be a valid url to a open
  //     document template
  public interface IOpenDocumentService
  {
    event EventHandler<OpenDocumentArgs> OnOpenDocument;

    byte[] AsByteArray(IObject vmroot, string viewModelName, out string reportname);
    void ExecuteOnOpenDocument(IObject vmroot, string viewModelName, byte[] openDocumentData, string reportname);
  }
}

 

In WECPOF for WPF we do this:

      File.WriteAllBytes(suggestedfilename, openDocumentData);
        System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo(suggestedfilename);
        System.Diagnostics.Process.Start(sInfo);

In ASP.NET you would do something else.

If you do not want to open the file – just generate the data within – then use new EAL operator vSomePumpRev.opendocumentreportasblob(‘PumpRevDeepReport’)

A bit hasty and vauge

So you might think I just skimmed over stuff – like how to get hold of the place holder tags that are replaced with data?

This you do by entering the tag %meta% in your template. We will always look for this tag – and when found we will add all valid tags in its place.

How do you create hierarchical structure in the report? Like this in the data:

image

And like this in the template:

image

The tag %%+Name% acts as row builder, the following tag %OtherName% is the data in the child. In the Example: %%+ComponentSpecificationRevs% – I stick this as the first thing in a table row and the row will be duplicated for each child. Then the %RevisionNumber% is filled in in the cell.

The reporting mechanism also works for excel. Example excel result report:

image

Update 2014-03-06. Qualifications

When working with reports we sometimes do not know in design time what the needs for the report is at runtime. To handle these situation the template tagging has been extended to allow for qualifications. Let me explain. Smallest possible report sample:

image

image

 

image

We want to allow for picking the correct Class2 in runtime time, while working on the template.

If we have this excel template:

image

we get this data out:

image

The qualification extension is that we now can have Template tags like this:

%Class2[Name=Hello1]Name%

What this mean is that we are navigating to ViewModel column Class2 – but this is a list – and filter the result on the viewmodel column Name of ViewModelClass Class2 – taking the one with value “Hello1” – for that Class2 we use the Name column… Example:

image

will give you:

image

(Notice that we have different external ids in the two last columns – the first from a Class2 with Name==Hello1, the other from one with Name==Hello2)

This is useful when you have data in name value pair patterns.

Update 2014-04-04

Images in Word reports? This is how.

Add some placeholder image in the template:

image

On the Image “Format Picture” – Alt Text property – enter the Tag that holds the image blob in your viewmodel:

image

Now – the image will be replaced with your data. And new from today is also the fact that the Aspect ratio of your data is kept in the final word (odt) report:

image

This entry was posted in OCL, Reports, ViewModel. Bookmark the permalink.

6 Responses to Microsoft office and OpenDocument as a Report generator

  1. guest00 says:

    Love ECO!!!

  2. fgabor says:

    I need some help!
    With the default ECO-model (Class1-Class2) the template below in Excel don’t generate hierarchical report:
    | %%+AllInstances%%Attribute1% | %%+Class2%%Name% |
    and the output like this:
    | aaa | %%+Class2%%Name% |
    | bbb | %%+Class2%%Name% |
    With Word the report work fine. What is wrong?

    • Admin says:

      Excel does not handle hierarcical data. Word manages since it allows nested tables. Excel is just one big table.
      Instead you must design some “squared” data with the viewmodel.
      Like a viewmodel that has a list: Class2.allinstances , and a nesting with columns self.Class1.Attribute1 and self.Name

      The perceived hierarcial data in the image above is actually done with a transient class that derives values from the real classes and this way make the result squared to fit excel

  3. guest00 says:

    Hi,
    Question about Images in Word reports. I’ve never tried images in the nested tables. Images works perfect but there are some difficulties to show images in the nested tables – I’m able to see rows built by the tag %%+Name% with all required data, but the image in all rows is the same – last image from the list.
    Is there any trick for image property in odt?

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

*