If I undestand correctly your question, you want to create an excel spreadsheet when an asynchronous call is made to your controller (so via AJAX).
First of all, I don't think it's a good idea to download file via ajax call. If your problem is a long time to generate your excel spreadsheet, I suggest you to generate it asynchronously (in a separate thread for example) and than to download the generated file via a classical GET request.
I'm doing something similar in a web application that generate a large CSV file. I'm using this simple method to get the generated file:
@RequestMapping(value = "/file", method = RequestMethod.GET)
public void getFile(
@RequestParam(value = "token", required = false) String token,
HttpServletResponse response) throws Exception {
// my logic...
String formattedDataString = exportTask.getResults());
response.setHeader("Content-Disposition", "attachment; filename=\""
+ exportTask.getExportFileName() + "\"");
response.setContentType("text/plain; charset=utf-8");
IOUtils.copy(
new ByteArrayInputStream(formattedDataString.getBytes()),
response.getOutputStream());
response.flushBuffer();
}
}
In my page I have a simple link like this:
<c:url var="fileUrl" value="results/file">
<c:param name="token" value="${token}" />
</c:url>
<a href='<c:out value="${fileUrl}" />'>Get File</a>
In my case the token
param identify the generated file, that is stored in the local DB.
However, if you still want to use ajax, I suggest you to take a look to @ResponseBody
annotation.