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

Scout/HowTo/3.9/Creating Word Reports with Docx4j

< Scout‎ | HowTo‎ | 3.9
Revision as of 14:34, 3 July 2013 by Matthias.zimmermann.bsiag.com (Talk | contribs) (Create Word Reports)

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 the Word template file into the template field TemplateField. 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;
   }

Back to the top