Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring MVC 3 download an Excel file

I can not manage to download an excel file from the browser even though there is no error here 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 got the information request 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 content of response

 ��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 gives me the choice to sauvgader the file in a folder.

thank you

like image 559
badri Avatar asked Jun 19 '13 16:06

badri


People also ask

How do I download an XLSX file?

To do this: Select File > Save As > Download a Copy. If Excel asks whether to open or save the workbook, select Save. Note: If you select Open instead of Save, the workbook will open in Protected View.

How can I download XLS file in Java?

file = new File(home + "/Downloads/" + "excel" + filename + ". xls"); Runtime. getRuntime(). exec("cmd.exe /C start " + file);


1 Answers

A bit late to the party but...

I'm guessing the error is in this line:

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

try changing "attachment" to "inline" like this:

header.set("Content-Disposition", "inline; filename=test.xls");
like image 125
JMB Avatar answered Sep 22 '22 14:09

JMB