Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/3.9/Creating Word Reports with Docx4j"

< Scout‎ | HowTo‎ | 3.9
(Mapping Content into Word Templates)
(Mapping Content into Word Templates)
Line 83: Line 83:
  
 
When we look at the corresponding Word template file, it becomes clear how the content is matched with the template.
 
When we look at the corresponding Word template file, it becomes clear how the content is matched with the template.
Standard doc variables, such as { DOCVARIABLE CompanyName \* MERGEFORMAT } in the screenshot below may simply be filled in with the following line of code.
+
Standard doc variables, such as '''{ DOCVARIABLE CompanyName \* MERGEFORMAT }''' in the screenshot below may simply be filled in with the following line of code.
  
 
   variables.put("CompanyName", "BSI Business Systems Integration AG");
 
   variables.put("CompanyName", "BSI Business Systems Integration AG");

Revision as of 16:09, 3 July 2013

This how-to describes how to:

  • Load a Microsoft Word (docx) template file
  • Load content into it's docvariables
  • How to pouplate tables
  • How to export the resulting Word document (report)

This how-to makes use of the Docx4j support provided on the Eclipse marketplace.

Activate the Docx4j Support for a Scout Application

The simplest way to integrate the Docx4j support in a Scout application is to tick the checkbox Docx4j Support in the Scout Object Properties of the Scout application as shown in the screenshot below.

Download docx4j.png

The SDK will then download the docx4j component and update the Scout project setup accordingly. Leave all default settings and accept the suggested proposals.

Create Word Reports

The simplest way to demonstrate the report generation feature is to add a string field and a button to a test form in your application. In the code below, it is assumed that the string field is called TemplateField and the button RunReportButton.

   @Order(20.0)
   public class RunReportButton extends AbstractButton {
   
     @Override
     protected String getConfiguredLabel() {
       return TEXTS.get("RunReport");
     }
   
     @Override
     protected void execClickAction() throws ProcessingException {
       File template = new File(getTemplateField().getValue());
       File report = runReport(template);
       
       // opens the filled in report using the default application to open this type of fields
       SERVICES.getService(IShellService.class).shellOpen(report.getAbsolutePath());
     }
   }

In execClickAction we first get the template field. Where the user needs to provide the absolute path to a Word template file (you may use this Media:Invoice_template_sample.zip file) into the template field TemplateField. For example:

   C:/templates/invoice_template_sample.docx

Then in method runReport the report is prepared and shown using the shellOpen method which is available per default in any Scout application created with the Scout SDK. Now, let's look at the code to create the report.

   private File runReport(File template) throws ProcessingException {
     //  copy original template file into temp file
     File tmpFile = IOUtility.createTempFile(IOUtility.getTempFileName(".docx"), IOUtility.getContent(template.getPath()));
     DocxAdapter docxFile = new DocxAdapter(tmpFile);
     Map<String, String> variables = new HashMap<String, String>();
     
     // populate doc variables
     variables.put("Name", "John Doe");
     variables.put("Phone", "(123) 456 78 90");
     variables.put("CompanyName", "BSI Business Systems Integration AG");
     variables.put("Email", "john.doe@bsiag.com");
     variables.put("CompanyAddress", "Täfernstrasse 16a, 5405 Baden");
     variables.put("InvoiceNo", "No. 2013-007");
     variables.put("InvoiceDate", DateUtility.formatDate(new Date()));
     variables.put("BillingName", "Jane Smith");
     variables.put("PayableToName", "John Doe, BSI");
     variables.put("SubTotal", "$1,530.00");
     variables.put("SalesTax", "$229.50");
     variables.put("Shipping", "$250.00");
     variables.put("Total", "$2,009.50");
     
     // and content for embedded table
     Object[][] orderItems = new Object[][]{
         new Object[]{"1", "Table", "$800.00", "$800.00"},
         new Object[]{"4", "Chair", "$150.00", "$600.00"},
         new Object[]{"1", "Assembling", "$130.00", "$130.00"},
     };
     
     // fill in doc variables, table content and create output file
     docxFile.setFields(variables);
     docxFile.fillTable(1, 1, 0, orderItems);
     File reportFile = docxFile.save();
     
     return reportFile;
   }

Mapping Content into Word Templates

When we look at the corresponding Word template file, it becomes clear how the content is matched with the template. Standard doc variables, such as { DOCVARIABLE CompanyName \* MERGEFORMAT } in the screenshot below may simply be filled in with the following line of code.

 variables.put("CompanyName", "BSI Business Systems Integration AG");

where variables is of type Map<String, String>

Word template file.png

The Result

Using the code and template above you may get the following output.

Docx4j report generation.png

Back to the top