Spring MVC 3 download Excel file

I am unable to download the excel file from the browser, although there is no error in my controller method.

@RequestMapping(value = "/getExcelFile", method = RequestMethod.GET, produces="application/json") public @ResponseBody HttpEntity<byte[]> getUserDetailsExcelFile(@RequestParam Long id) { try { byte[] wb = ExcelReportView.buildExcelDocument(data); HttpHeaders header = new HttpHeaders(); header.setContentType(new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet")); header.set("Content-Disposition", "attachment; filename=test.xls"); header.setContentLength(wb.length); return new HttpEntity<byte[]>(wb, header); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } 

I received a request for information from firebug.

 Request Method:GET Status Code:200 OK Request Headersview source Accept:application/json, text/plain, */* Accept-Encoding:gzip,deflate,sdch Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 Connection:keep-alive Cookie:org.cups.sid=ed8f7540d976ccc90b85954f21520861; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=fr; __ngDebug=true; JSESSIONID=tqi3ofownl17 Host:localhost:8888 Referer:http://localhost:8888/ User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 X-Requested-With:XMLHttpRequest Query String Parametersview sourceview URL encoded id:1 Response Headersview source Cache-Control:no-cache Content-Disposition:attachment; filename=test.xls Content-Length:1849 Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 

and the content of the answer

    A    \p B a=   =h\: # 8X@  "  1   Arial1   Arial1   Arial1   Arial"$"#,##0_);("$"#,##0)"$"#,##0_); [Red]("$"#,##0) "$"#,##0.00_);("$"#,##0.00)% "$"#,##0.00_);[Red]("$"#,##0.00),*'_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)5)0_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)4,/_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)=+8_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)                                                                                                             +       )       ,       *                                `   Users Details   c First Name Last NameEmailPhone AddressLast AppointmentAppointment Status  K    d    MbP?_*+  %      "d,, ? ?U           v> @e 

So, I do not know what to do, I want the browser to give me a choice for the sauvgader file in the folder.

Thank you

+5
source share
2 answers

A little late, but ...

I assume the error in this line is:

 header.set("Content-Disposition", "attachment; filename=test.xls"); 

try changing the "attachment" to "inline" as follows:

 header.set("Content-Disposition", "inline; filename=test.xls"); 
+1
source

So I don’t know what to do, I want the browser to give me a choice to save the file in a folder.

It was very late to the party, but ... I was in the same situation: the server creates Excel, the response to the browser is fine, but the dialog does not appear.

The problem is that I am making an Ajax call. If I call the Excel URL in a new browser window, I can download it using the Save As dialog box.

So I created a hidden <a> element in html with the download html5 attribute. Then, in the ajax callback, I set the href attribute of the <a> tag. Finally, I call the click () function to start the download.

Like that:

 function openSaveAsDialog(){ var aElement= document.getElementById("aElementId"); aElement.href = "path/to/excel/documentId"; aElement.click(); } 

and in HTML

 <a id="aElementId" href="path_to_excel" download="filename_to_download" ng-show="false">Download file</a> 

Hope this helps.

PS: this dirty solution works for me, but if someone can show if there is a better solution for launching the save as dialog without a hidden HTML element, I will be happy.

0
source

Source: https://habr.com/ru/post/1487086/


All Articles