0

I'm trying to excel export from popup window(jquery UI Dialog).I have java class method with response type content-disposition attachment header.But its not downloading the excel sheet.

jQuery("#dialog-form").dialog ({ 
 autoOpen: false,
  height: 600,
  width: 700,
  modal: true,
  resizable: false,
  draggable: false, 
  buttons : {
"Export" : function() {
  jQuery.ajax({
     url : '<s:url action="list" method="export"/>',
     });
  } });

Java class :

public String export(){

--some backend calls.

httpServletResponse.setHeader("Content-disposition",
      "attachment; filename="+filename+".xls");
      ServletOutputStream outputStream = httpServletResponse.getOutputStream();
      outputStream.flush();
      return null;
    }

Console :

No result returned for action at null

Please advise why is this excel download is not working ?

user2444474
  • 623
  • 8
  • 21
  • 40

1 Answers1

1

There are some errors:

  1. filename should be enclosed in double quotes, and inline should be used instead of attachment, if you want to open it in the browser instead of downloading it:

    httpServletResponse.setHeader("Content-disposition", 
                                  "inline; filename=\""+filename+"\".xls");
    
  2. You should always specify the contentType:

    httpServletResponse.setContentType(
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    

    (or application/vnd.ms-excel for older Excel files);

  3. from the Action you should return NONE instead of null (this is causing your error).


That said, you really don't have the need to use an Action like a Servlet (like instead I had here):

just use a Stream result, set the ContentType and the ContentDisposition in the Struts.xml, and return the InputStream to the page, like described in this example.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for the hint using the "contentType". I still had the vnd.ms-excel but using a dialog within dataTables didn't allow me that any more. – leole Dec 08 '17 at 11:37
  • You're welcome @leole. Feel free to upvote the answer if it helped – Andrea Ligios Dec 08 '17 at 12:01