<div dir="ltr" style="text-align: justify;" trbidi="on">In this tutorial we will discuss about download ms excel files from <b>JAX-RS</b>, for user to download a file, annotate the method with<i><b> @Produces(&#8220;application/vnd.ms-excel&#8221;) :</b></i></p>
<ol>
<li>Put <i><b>@Produces(&#8220;application/vnd.ms-excel&#8221;)</b></i> on service method, with a Response return type. It means the output is <b>a ms excel file.</b></li>
<li>Set &#8220;<b>Content-Disposition&#8221;</b> in Response header to tell browser pop up a download box for user to download.</li>
</ol>
<p><b>1. Download Excel file in <i>JAX-RS</i></b></p>
<p> Full example to download an<b> ms excel </b>file from<i><b> JAX-RS.</b></i></p>
<pre class="highlight" name="code">package com.dineshonjava.ws.rest; 
 
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; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Path("/excel") 
public class ExcelFileService { 
 private static final String EXCEL_PATH = "d:Contact.xls"; 
 
 @GET 
 @Path("/get") 
 @Produces("application/vnd.ms-excel") 
 public Response getContactExcelFile() { 
 
 File file = new File(EXCEL_PATH); 
 
 ResponseBuilder response = Response.ok((Object) file); 
 response.header("Content-Disposition", 
 "attachment; filename="contact_excel_file.xls""); 
 return response.build(); 
 
 } 
} 
 
</pre>
<p>
<b>2. Test</b></p>
<p> Deploy above <b>JAX-RS</b> service, access this URI pattern :<i><b> &#8220;http://localhost:8181/sdnext/doj/excel/get&#8221;</b></i>.</p>
<p> Figure : <b>MS-Excel</b> file <i><b>&#8220;d:Contact.xls&#8221;</b></i> from server is prompt for user to download, with a new me excel file name <i><b>&#8220;contact_excel_file.xls&#8221;</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/06/downloadexcel.png"/></div>
<div align='center' id="ads-id"></div>
<p><b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Download%20excel%20file%20from%20JAX-RS.zip?attredirects=0&;d=1" target="_blank">Download excel file from JAX-RS.zip</a></b></p>
<p>
<i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank">Wikipedia for REST Web Service</a></b></i></p>
<p> ; </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/06/download-pdf-file-from-jax-rs.html#.UcHoJJyWyqk">Previous </a><;<; ; ; || <a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/06/download-excel-file-from-jax-rs.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/download-pdf-file-from-jax-rs/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/what-is-rest-and-rest-architecture-and-rest-constraints/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '415'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…