Microsoft office and OpenDocument as a Report generator


Update: 2024.07.01

Generate Reports Using OpenDocument and Microsoft Office

OpenDocument format is an open file format standard for office applications compatible with Microsoft Office and open source applications like LibreOffice and OpenOffice.

Common filename extensions used for OpenDocument documents are:

  • .odt for text documents
  • .ods for spreadsheet documents

MDriven applications allow generating reports from model-driven data using OpenDocument format.

Text Document

1. Start by creating an OpenDocument text document using any Office application that supports OpenDocument format.

2. Add %meta% tag within the document as this will be used to print out all the available tags within your ViewModel for printing out model data.

NOTE: Make sure to write tags without spaces between the word (meta) and the percentage (%) signs.

Saving Strategies

a) Temporary Location
(i) Create a folder named temp in your C:/ directory and save your file in the directory as mytemplate.odt.
(ii) Your url path will now be 'c:\\temp\\mytemplate.odt'.
b) Permanent Location (AssetsTK Strategy)
(i) This strategy allows your template document to be uploaded with your model onto the server running your Turnkey application during deployment.
(ii) Go to the location where your .modlr file is saved.
(iii) Create a folder with the name in the format of <YouModelFileName>_AssetsTK where <YouModelFileName> is the name of your .modlr file name.
(iv) Within the folder create another folder named content where your save your template document.
(v) Your url path will now be 'http://localhost:8182/content/mytemplate.odt'
(vi) Check here on how to have a dynamic url for when your deploy your application onto the Mdriven Server.

3. Create a ViewModel with the Name OpenDocumentReportTemplate. This ViewModel will be used as a template for the Model-Driven data to generate reports.

4. Within the ViewModel context menu, click Add column > Add columns needed for your report or create columns TemplateUrl and ReportFileName within the ViewModel.

5. Select the ViewModel to view the settings on the right. Uncheck the Use placing hints section at the top.

6. In the TemplateUrl column expression, enter the url path created earlier using any of the saving strategies above.

In the ReportFileName column expression, enter a file name for the new OpenDocument that will be generated in the format '<FileName>.odt' where <FileName> is the name of your file.

Create a class action within the ViewModel whose data you want to print out using the expression below:

self.opendocumentreportshow(<classname>.ViewModels.OpenDocumentReportTemplate)

where <classname> is the root class of the ViewModel.

Within OpenDocumentReportTemplate, add columns whose data you want to print out.

Trigger print out using the created class action. The first print out will return all tags available for printing out data. Use these tags to format the structure of your report document.

If you don't want to use the %meta% tag to print out the available tags first, you may use the column names directly with the same expression %<ColumnName>%.

To print out nested ViewModel data in a table format, create a table and add the expression in the following format:

%%+<Nested ViewModel Column Name>%%<First Column Name>% %<Column Name>% %<Column Name>%

ViewModel Template for Printing Out Text Open Document

ViewModel Template for Printing out Model-Driven data to OpenDocument reports

Text Document Template

Located at <YourModelFileName>_AssetsTK/Content/mytemplate.odt and accessible at http://localhost:8182/content/mytemplate.odt

Office Text Document for Printing out model-driven data using OpenDocument format

The table format is for both text documents and spreadsheet documents.

When using Microsoft Office Word, check here for format styling that is supported in OpenDocument format.

Spread Sheet Document

1. Start by creating an OpenDocument spread sheet document using any Office application that supports OpenDocument format.

2. Add %meta% tag within the document as this will be used to print out all the available tags within our ViewModel for printing out model data or use the column names directly with the same expression %<ColumnName>% where <ColumnName> represents the column name in the ViewModel.

3. Use .ods extension for spread sheet documents and your url path will now be 'http://localhost:8182/content/mytemplate.ods'.

ViewModel Template for Printing Out Spread Sheet Open Document

Spread Sheet Document ViewModel Template

Adding _float at the end of a column name ensures that the output in Excel is processed as a number.

Spread Sheet Template

Located at <YourModelFileName>_AssetsTK/Content/mytemplate.ods and accessible at http://localhost:8182/content/mytemplate.ods

Spread Sheet Document Template

Sample Spread Sheet Output

SpreadSheet Sample Output

Other Pages On Printing Out Documents:

Older Information - 2018.04.01

Because we thought 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 to do it.

Create a Word template:

Reporting.png

You can have grids in grids to create structure:

Reporting 1.png

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

Now you need data. Declare a ViewModel in Modlr:

Reporting 2.png

(By now, you know that a ViewModel transforms your model for a specific need – in this case, the OpenDocument Report.)

Reporting 3.png

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

Reporting 4.png

Make the TemplateUrl column return the URL to where your template from above can be found – maybe in some SharePoint instances, 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 the new EAL operator:

vSomePumpRev.opendocumentreportshow(‘PumpRevDeepReport’) – this will call the OnOpenDocument event on the new IOpenDocumentService. This happens when each UI platform has its own way of showing 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 – generate the data within – then use the new EAL operator vSomePumpRev.opendocumentreportasblob(‘PumpRevDeepReport’)

A Brief Recap (A Bit Hasty and Vague)

So you might think I just skimmed over stuff – like: how do you get a hold of the placeholder tags that are replaced with data?

Do this 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 their places.

How do you create a hierarchical structure in the report? Find it like this in the data:

Reporting 5.png

And like this in the template:

Reporting 6.png

The tag %%+Name% acts as a 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. An example of an Excel result report:

Reporting 7.png

Update 2014-03-06. Qualifications

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

Reporting 8.png

Reporting 9.png

Reporting 10.png

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

If we have this Excel template:

Reporting 11.png

We get this data out:

Reporting 12.png

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

%Class2[Name=Hello1]Name%

What this means 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:

Reporting 13.png

will give you:

Reporting 14.png

(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 you can do it.

Add some placeholder images in the template:

Reporting 15.png

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

Reporting 16.png

Later, in Office(Word), it will look like this:

2022-05-27 17h19 23.png

Now, the image will be replaced with your data. Also, new today is the fact that the Aspect ratio of your data is kept in the final Word (odt) report:

Reporting 17.png

The MDriven Book - Next Chapter: Prototyping

This page was edited 51 days ago on 10/02/2024. What links here