Download excel file from JAX-RS

In JAX-RS, for excel file, annotate the method with @Produces("application/vnd.ms-excel") :

  1. Put @Produces(“application/vnd.ms-excel”) on service method.
  2. Set “Content-Disposition” in Response header to prompt a download box.

1. Download Excel file in JAX-RS

Full example to download an excel file from JAX-RS.


import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/excel")
public class ExcelService {

	private static final String FILE_PATH = "c:\\excel-file.xls";

	@GET
	@Path("/get")
	@Produces("application/vnd.ms-excel")
	public Response getFile() {

		File file = new File(FILE_PATH);

		ResponseBuilder response = Response.ok((Object) file);
		response.header("Content-Disposition",
			"attachment; filename=new-excel-file.xls");
		return response.build();

	}

}

2. Demo

Access this URI pattern : “/excel/get“.

Figure : Excel file “c:\\excel-file.xls” from server is prompted for user to download, with a new file name “new-excel-file.xls

download excel file from server

Download Source Code

References

  1. JAX-RS @Produces JavaDoc
  2. list of the Application types

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Challa Rao Ande
Guest
Challa Rao Ande

I’m implementing end points as stateless ejbs, so I’m getting not serializable exception on BuiltResponse class. Any ideas? I’m using rest easy.

Srinivasulu Dagda
Guest
Srinivasulu Dagda

Can you please tell how can we integrate this service through Angular JS

Gabriel
Guest
Gabriel

can you please also write a Jersey client for this service? Thank you.

MadMarge
Guest
MadMarge
I tried a simple web service to try this out and it’s “working” (meaning the code isn’t blowing up) but the browser isn’t liking it. Here’s my code (using a later version of Spring and jersey): @RequestMapping(value=”xls-test”, method = {RequestMethod.GET}, produces=”application/vnd.ms-excel”) public @ResponseBody Response xlsTest( Model model) { try { File file = new File(“/Users/Jeff/Desktop/test.xls”); ResponseBuilder response = Response.ok((Object) file); response.header(“Content-Disposition”, “attachment; filename=”test.xls””); LOG.debug(“File name = ” + file.getAbsolutePath()); return response.build(); } catch (Exception e) { return null; } } Hitting it with a browser produces: HTTP Status 406 – type Status report message description The resource identified by this… Read more »
Shiv
Guest
Shiv

Hey can you please let me know how you can call this rest service from Jquery/Javascript. I am doing an AJAX call but the data format returned isnt correct and it has a lot of spl characters.
When I fire the URL directly in the browser, it downloads the Excel file correctly.

$.ajax({
url:”../control/limitProfile/exportIntradayTradeList/” + gid
}

ram
Guest
ram

I am unable to open the apache poi xls in mac os preview
please help on this

sandeep bhowmik
Guest
sandeep bhowmik

sir,
i want to know how to create a simple soap webservice that return a xml file or a text file, using netbeans.

jfrancois
Guest
jfrancois

Hello,

I would like to know how download an excel file jax-rs with a query jpql

Vijay
Guest
Vijay
Hi, Have written a similar Restful service using Jersey Framework to render xls file upon request. However am facing below problem while running the service url on a new IE tab [IE version 8]. We are using single-signon authentication mode. The first request gives us the excel report. However if open a new tab in the same IE window and request the URL, it craps out with below error “Internet Explorer cannot download xxxx from server Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please tyr again later”… Read more »
Sergey
Guest
Sergey

Do you have an example how to call it from java script or better jQuery. I can execute it directly from browser but not from web page.