Build Reports using CSV Data Sources in GroupDocs.Assembly for Java 19.10

We have recently released version 19.10 of GroupDocs.Assembly for Java that brings an exciting feature of building reports using CSV data sources. Along with this, we have simplified the process of working with XML data sources. In this article, I’ll show you how to populate document templates using CSV as well as XML data sources.

Working with CSV Data Source

The Comma Separated Values (CSV) is a file format for storing the data in the form of plain text where the values are separated by commas. CSV is widely used for exchanging data among the applications and therefore, we have added the support of CSV to be used as a data source.

We have introduced the CsvDataSource class to access the CSV data sources where its instance is passed to the DocumentAssembler. Although CSV as a format does not define a way to store values other than of the string type, CsvDataSource is capable of recognizing values of the following types by their string representations:

  • Integer
  • Long
  • Double
  • Boolean
  • Date

For recognition of data types, string representations of corresponding values must be formed using invariant culture settings.

Let’s have a look at how a CSV data source can be used for populating the templates. In the template documents, a CsvDataSource instance should be treated in the same way as if it was a DataTable instance. The following are the sample CSV, the template and the code to populate the template.

CSV

John Doe,30,1989-04-01 4:00:00 pm
Jane Doe,27,1992-01-31 07:00:00 am
John Smith,51,1968-03-08 1:00:00 pm

Template

<<foreach [in persons]>>Name: <<[Column1]>>, Age: <<[Column2]>>, Date
of Birth: <<[Column3]:"dd.MM.yyyy">>
<</foreach>>
Average age: <<[persons.average(p => p.Column2)]>>

Code

The following is the report that will be generated as a result.

Name: John Doe, Age: 30, Date of Birth: 01.04.1989
Name: Jane Doe, Age: 27, Date of Birth: 31.01.1992
Name: John Smith, Age: 51, Date of Birth: 08.03.1968
Average age: 36

In this example, we have used Column1, Column2 and so on for the column names in the template. However, if the first row in the CSV file contains the column names, you can configure CsvDataSource to treat the first row as column names. The CSV, template, and code, in this case, would be the following:

CSV

Name,Age,Birth
John Doe,30,1989-04-01 4:00:00 pm
Jane Doe,27,1992-01-31 07:00:00 am
John Smith,51,1968-03-08 1:00:00 pm

Template

<<foreach [in persons]>>Name: <<[Name]>>, Age: <<[Age]>>, Date of
Birth: <<[Birth]:"dd.MM.yyyy">>
<</foreach>>
Average age: <<[persons.Average(p => p.Age)]>>

Code

Working with XML Data Sources

To access XML data while building a report, you can use the facilities of DataSet to read XML into it and then pass it to the assembler as a data source. However, if your scenario does not permit to specify XML schema while loading XML into DataSet, all attributes and text values of XML elements are loaded as strings. Even when XML schema is not provided,
XmlDataSource is capable of recognizing values of the following types by their string representations:

  • Integer
  • Long
  • Double
  • Boolean
  • Date

The following are different scenarios to deal with the XML data sources:

1. If a top-level XML element contains only a sequence of elements of the same type.

In this case, an XmlDataSource instance should be treated in the same way as if it was a DataTable instance.

XML

<Persons>
   <Person>
      <Name>John Doe</Name>
      <Age>30</Age>
      <Birth>1989-04-01 4:00:00 pm</Birth>
   </Person>
   <Person>
      <Name>Jane Doe</Name>
      <Age>27</Age>
      <Birth>1992-01-31 07:00:00 am</Birth>
   </Person>
   <Person>
      <Name>John Smith</Name>
      <Age>51</Age>
      <Birth>1968-03-08 1:00:00 pm</Birth>
   </Person>
</Persons>

Template

<<foreach [in persons]>>Name: <<[Name]>>, Age: <<[Age]>>, Date ofBirth: 
<<[Birth]:"dd.MM.yyyy">>
<</foreach>>
Average age: <<[persons.Average(p => p.Age)]>>

Code

2. If a top-level XML element contains attributes or nested elements of different types.

In this scenario, an XmlDataSource instance should be treated in template documents in the same way as if it was a DataRow instance.

XML

<Person>
   <Name>John Doe</Name>
   <Age>30</Age>
   <Birth>1989-04-01 4:00:00 pm</Birth>
   <Child>Ann Doe</Child>
   <Child>Charles Doe</Child>
</Person>

Template

Name: <<[Name]>>, Age: <<[Age]>>, Date of Birth:
<<[Birth]:"dd.MM.yyyy">>
Children:
<<foreach [in Child]>><<[Child_Text]>>
<</foreach>>

Code

For more details on how to use CSV as well as XML data sources and create the document templates, please visit this documentation article. Try out all these features at your end by downloading or cloning the examples project from GitHub. If you find anything confusing for you, do let us know via our forum.

Share on FacebookTweet about this on TwitterShare on LinkedIn