【问题标题】:Excel export issue - from Jquery dialogExcel 导出问题 - 来自 Jquery 对话框
【发布时间】:2013-07-12 00:09:33
【问题描述】:

我正在尝试从弹出窗口(jquery UI 对话框)导出 excel。我有带有响应类型内容处置附件标题的 java 类方法。但它没有下载 excel 表。

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 类:

public String export(){

--some backend calls.

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

控制台:

No result returned for action at null

请告知为什么这个 excel 下载不起作用?

【问题讨论】:

    标签: jquery jakarta-ee struts2 datatables struts2-jquery


    【解决方案1】:

    有一些错误:

    1. 文件名应该用双引号括起来,如果你想在浏览器中打开而不是下载它,应该使用inline而不是attachment

      httpServletResponse.setHeader("Content-disposition", 
                                    "inline; filename=\""+filename+"\".xls");
      
    2. 您应该始终指定contentType

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

      (或 application/vnd.ms-excel 用于较旧的 Excel 文件);

    3. 您应该从 Action 返回 NONE 而不是 null(这会导致您的错误)。


    也就是说,你真的不需要使用像 Servlet 这样的 Action (like instead I had here):

    只需使用Stream result,在Struts.xml 中设置ContentTypeContentDisposition,然后将InputStream 返回到页面like described in this example

    【讨论】:

    • 感谢使用“contentType”的提示。我仍然有 vnd.ms-excel,但是在 dataTables 中使用对话框不再允许我这样做了。
    • 不客气@leole。如果有帮助,请随时为答案投票
    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 2011-05-28
    • 2018-06-17
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多