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 applications. As a developer, we often need to convert the large CSV data into a presentable format. This article will guide you to convert CSV data into PDF and MS Word reports in Java using a simple template.

The following topics are covered below:

Report Generation Java API

GroupDocs.Assembly for Java is the report generation API that I have used in this article to generate reports from the selected CSV data and a template in TXT format. It also supports report generation automation from multiple data sources like JSON, XML, and also from MS Word, Excel, and PowerPoint files as data files.

Download or Configure

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-assembly</artifactId>
        <version>21.4</version> 
</dependency>

Generate PDF Report from CSV Data in Java

Let’s start with the transformation of data into presentable PDF. The following steps will lead you to convert CSV data into a formatted PDF report.

  • Load CSV data source
  • Define template according to the CSV data
  • Provide CSV data source and template to simple method to generate PDF report.
CSV to PDF Report in Java

CSV Data

For the PDF report generation, I will be using the following sample CSV data of different persons along with their respective ages and date of birth.

Name,Age,Birth  
John Doe,32,4/1/1989 16:00  
Jane Doe,29,1/31/1992 7:00  
John Smith,53,3/8/1968 13:00

Template

Define the following template in TXT or DOCX format. This allows iterating the list of persons with their details. After that, you can jump to the code for report generation.

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

Java Steps to Generate PDF Report from CSV

The following steps explain the automatic conversion of CSV data into a PDF report according to the defined template.

The following code shows how to convert CSV data to PDF report in Java.

Generate MS Word Report from CSV data in Java

It is very similar to the above PDF report generation, you can easily create the MS Word DOC/DOCX report from the CSV data:

  • Load the CSV data from file.
  • Defining the template in TXT or DOCX format.
  • Set the output report document format as DOC/DOCX.
  • The rest of the code will remain the same to generate MS Word DOCX report from the CSV data.

The following code shows how to convert CSV data to DOCX report in Java.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, you have learned to convert the CSV data into PDF and MS Word reports in Java. I hope you are now comfortable building your own Java-based application to generate reports by converting CSV data to PDF format. Similarly, you can generate reports using data sources like JSON and XML.

For more about the API, you can visit documentation and the GitHub repository. In case of further queries and ambiguities, contact the free support on the forum.

See Also