Excel导出功能 - 来自jquery对话框窗口

时间:2013-07-16 07:26:34

标签: java jquery jsp struts2 download

我需要从jQuery对话框窗口创建excel导出功能。我有锚链接,然后单击链接到struts动作类,从数据库中提取数据并导出。

我正在使用jQuery调用方法,如:

jQuery.ajax({
     url : '<s:url action="partexport"/>',
     data : "filters.productNbr" : $("#productsTextArea1").val()
});

Struts动作类:

public String method {
    -----------------
    method to call backend...
    --------------------------
    httpServletResponse.setContentType("application/vnd.ms-excel");
    String filename = filters.getPeriod() + "_" +filters.getRegion() ;
    httpServletResponse.setHeader("Content-disposition",
    "attachment; filename="+filename+".xls");
    ServletOutputStream outputStream = httpServletResponse.getOutputStream();
    outputStream.flush();
    return NONE; 
}

它会返回null,但可以下载xls表。这里有可能实现Excel导出功能吗?

1 个答案:

答案 0 :(得分:0)

将二进制流返回到操作的输出:

String filename = filters.getPeriod() + "_" +filters.getRegion() ;
httpServletResponse.setContentType("application/octet-stream");
httpServletResponse.setHeader("Content-Disposition", "attachment; filename="+filename+".xls");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
Path fileIn = Paths.get(filename);
Files.copy(fileIn, outputStream); 
outputStream.flush();
return NONE;