Skip to content

Micro service to generate XLS files from a template and data which is in JSON format

License

Notifications You must be signed in to change notification settings

SEL-Columbia/json-to-xls

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

##JSON-to-XLS SERVICE

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:

  • Gradle for build
  • jxls internally to generate the XLS files
  • JsonToJava from this repo which converts json structure to equivalent Java Classes

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:https://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:https://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:https://localhost:8080/xls/{c2e39e3699214316a08af350c3969cbf} downloads the generated excel that maps to the unique generated excel token c2e39e3699214316a08af350c3969cbf

###Validations done

  • When we POST a request as : /templates with an invalid excel template, an exception would be thrown saying that "Template is not a valid Excel"

  • When we POST a request as : /templates with an excel template and it was not successfully saved for some reason, an exception would be thrown saying that "Unable to save template due to internal error"

  • When we POST a request with a invalid template token in the URL as : /xls/invalidTemplateToken with JSON data as payload, an exception would be thrown saying that "Could not find a valid template for the given token. Token: invalidTemplateToken"

  • When we POST a request with a valid template token in the URL as : /xls/validTemplateToken with an empty JSON data, an exception would be thrown saying that "JSON data cannot be empty"

  • When we POST a request with a valid template token in the URL as : /xls/validTemplateToken with invalid JSON data, an exception would be thrown saying that "JSON is not valid"

  • When we POST a request with a valid template token in the URL as : /xls/validTemplateToken with a valid JSON data, and if there is an internal error for some reason, an exception would be thrown saying that "Unable to generate excel from template and JSON due to internal error"

  • When we POST a request with an invalid excel token in the URL as : /xls/invalidExcelToken, an exception would be thrown saying that "Could not find a valid excel for the given token. Token: invalidExcelToken"

About

Micro service to generate XLS files from a template and data which is in JSON format

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Java 100.0%