Download Excel RESTFul Service

Download Excel RESTFul Service

Create report in excel or csv format is very common to any project. If you are actively using RESTFul service in your project and get requirement to generate download functionality of excel sheet through browser you could use below example.

Note if you are in hurry to just need code to send excel sheet in response please use below:

  • Detail example: To show something working I will use Jetty server with maven and POI to generate and send excel sheet through RESTFul serivce. Please create maven project name: DownloadExcelRESTFulService below is project structure:

Download Excel RESTFul Service

  • All dependencies pom.xml:

  • spring-context.xml:

  • log4j2.xml:

  • To load all properties:

  • Main RESTFul resource class where all request will come and here we have one method downloadXLS to download XLS sheet:

  • Abstract class to generate XLSX sheet:

  • which extends to create XLSX sheet:

  • POJO class to store XLSX sheet data while creating sheet:

  • Main class to load spring context and start jetty server:

  • That’s it. To start the applicatoin right click run as java application you will see on console it will start Jetty server on port 10002 as below:

Download Excel RESTFul Service

  • Now we are ready to download XLSX report. Open your browser and enter below URL: http://localhost:10002/RESTFul/downloadxls you will see XLSX sheet downloaded as below:

Download Excel RESTFul Service

  • Open XLSX sheet to see its content:

Download Excel RESTFul Service

  • For more information on RESTFul service please visit Spring documentation here

download Download Project:  DownloadExcelRESTFulService

One thought on “Download Excel RESTFul Service

  1. File is getting downloaded successfully but could see one more file is getting created inside project folder.How to avoid that?

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *