onaio / json-to-xls

Micro service to generate XLS files from a template and data which is in JSON format
Other
5 stars 2 forks source link

JSON-to-XLS SERVICE

Note: Upgraded to jxls 2.*.* which is not compatible with jxls 1.*.*

This is a service that can be used to generate XLS files from given XLS template and data in JSON format

The following are the dependencies to run this project:

Firstly, you need to save an excel template. It can be done by POSTing to the API: /templates, with the excel template to be saved as the payload.

e.g., We can send sampleTemplate.xls as payload to http://localhost:8080/templates.

Here, the sampleTemplate.xls should be a valid excel file. If it is not valid, it throws an exception.

When the excel template is a valid one, the template is stored in postgres db in template table and returns a unique template token, uniqueTemplateToken which corresponds to that particular template. This token can be used to generate XLS files for the template referred by it.

Lets consider that POSTing the sampleTemplate.xls in our example returned a unique token something like 9afc1c3878e94f5eaf4bf2d2c5c25b40. We can use this token to refer to the sampleTemplate.xls later.

Next, we can generate the XLS files by POSTing to the API: /xls/uniqueTemplateToken with data in JSON format as the payload.

This will generate an Excel file using the template corresponding to the uniqueTemplateToken and JSON data. The generated Excel file is saved in postgres db in excel table. It returns a URL in the response, which can be used to download the excel file generated.

We can generate any number of XLS files for a given XLS template with different JSON data

In our example, POST http://localhost:8080/xls/9afc1c3878e94f5eaf4bf2d2c5c25b40 with the sample JSON data : { "loc": { "state": "Karnataka", "district": "Mysore" }, "ind": { "anc": "1", "anc_12": "2", "anc_jsy": "3" } }

will generate an excel by filling all these details in the sampleTemplate.xls. It returns the URL, /xls/c2e39e3699214316a08af350c3969cbf

Here, internally this JSON data is converted to Java objects using JsonToJava. These objects are used by XLSTransformer, a class that generates an Excel by inserting java objects in the sample template.

Now, we can download the excel generated by using the API: GET /xls/generatedExcelToken.

In our example, doing a GET request on http://localhost:8080/xls/{c2e39e3699214316a08af350c3969cbf} downloads the generated excel that maps to the unique generated excel token c2e39e3699214316a08af350c3969cbf

Validations done