1

我需要创建从 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 导出功能?

4

1 回答 1

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; 
于 2013-07-16T10:11:55.067 回答