You can present Microsoft Excel data in the form of customized reports to your users such as clients, stakeholders, etc. As a Java developer, you can generate such reports programmatically by using Excel spreadsheets as a table of data. In this article, you will learn how to generate reports from Excel data using Java.
The following topics are discussed/covered in this article:
Java API for Generating Reports
For generating the reports from Excel data, I will be using GroupDocs.Assembly for Java API. It enables you to build powerful document automation and report generation applications. It fetches data from the data source as per the defined template document, assembles it, and generates reports in the specified output format. The API supports fetching data from various data sources such as XML, Excel, JSON, and CSV. You can easily generate reports in all commonly used file formats such as PDF, HTML, and Microsoft Word.
You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned code examples.
<name>GroupDocs Java API</name>
Generate Reports from Excel Data using Java
You can easily generate reports in Word from your Excel data by following the simple steps mentioned below:
- Get Excel data source
- Define a template according to Excel data
- Convert Excel Data to Word Report in Java
Excel Data Source
You can use the tabular data as a data source provided in the Excel spreadsheet to generate reports. I will be using the following sample Excel data for generating the report. This is the contracts data of clients with their respective managers and the agreed contract price.
You can use a Linq-based template syntax to create a template. A template is composed of common document contents and tags describing the template’s structure and data bindings. You can define the following template in the DOCX or XLSX file. This template enables you to iterate the Contracts’ data and their respective managers with the contract price. Each group in the template has a unique key defined by the input selector and contains items of the source enumeration associated with this key. You can access the key of a group instance using the Key property. Once you have created the template, you can jump into code for generating the report. You can read more about template syntax in the documentation.
Convert Excel Data to Word Report in Java
You can automate the conversion of Excel data to the DOCX report based on the template by following the steps mentioned below:
- Define Excel data file, the template file, and DOCX output report file paths
- Create an instance of the DocumentTableOptions class
- Set setFirstRowContainsColumnNames to true
- Create the DocumentTable with defined Excel data file and DocumentTableOptions
- Create an instance of the DocumentAssembler class
- Call the assembleDocument() method with the provided Excel data and defined template
The following code sample shows how to generate a report from an Excel data source according to the defined template using Java.
The DocumentTableOptions class provides a set of options to control the extraction of data from a document table. I set the _FirstRowContainsColumnNames _property to true so that the column headers should not become part of the report data.
The **DocumentTable ** class provides access to the data of a single table (or a spreadsheet) located in an external document to be used while assembling a document.
The DocumentAssembler class provides various methods to generate reports using the defined template document with data. The assembleDocument**()** method of this class takes three input parameters, the defined template as source document, the output file path, and the data source. It populates the data from the data source based on the provided template document, and stores the resulting document to the target path. You can save the resulting document into various supported file formats such as Word, Excel, or HTML.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
In this article, you have learned how to generate reports from Excel data using Java. You have also learned how to create a report template to generate reports. You can learn more about GroupDocs.Assembly for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.