问题描述
我有一些来自JQGrid的数据应该导出到excel.因此,我们编写了一个Java Servlet,将数据写入excel并将其发送回.在客户端,我们通过发送JSON数据来使用AJAX JSONP请求.我能够找到该servlet,然后将servlet将创建的excel发送回客户端.但是我无法从客户端看到excel或任何形式的输出.
I have some data from JQGrid that should be exported to excel. So, we have written a java servlet to write the data to excel and send it back. From the client side we are using an AJAX JSONP request by sending JSON data. I am able to hit the servlet and servlet sending the created excel back to the client. But i am not able to see excel or any kind of output from client side.
当我使用Fiddler并观察到http调用时,我发现该应用程序收到了结果.但仍然没有显示结果.
When i use the Fiddler and observed the http calls, i found that application received the result. but still it is not showing the result.
这是我收到的结果标头.
Here is my result header, that i have received.
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=PistonData.xls
Content-Type: application/vnd.ms-excel
Content-Length: 6144
Date: Tue, 27 Mar 2012 08:49:04 GMT
如何使用JQuery以Excel形式打开此结果?有人可以建议我解决此问题的方法.
How to open this result as Excel using JQuery? Can somebody please suggest me a way to fix this issue.
更新#1 忘记包含请求
$.ajax({
type: "POST",
dataType: "jsonp",
contentType:'application/vnd.ms-excel',
url: "http://devmachine:9010/axis/SPSServlet",
data: param,
success: function (dataToSend) {
alert(dataToSend);
}
});
更新#2 根据Oleg的建议,我为这个问题找到了解决方案.
Update #2As per Oleg's suggestions i worked out a solution for this problem.
这是我的代码:
<form id="frmExcelExport" style="display:none;">
<input type=hidden id="partId" name="partId" />
<input type=hidden id="columnNames" name="columnNames" />
<input type=hidden id="data" name="data" />
</form>
$('#columnNames').val(colModStr);
$('#partId').val(currentPartID);
$('#data').val(dataStr);
var urlForExport = "http://devmachine:9010/axis/SPSServlet";
$('#frmExcelExport').attr("method", "post");
$('#frmExcelExport').attr("action", urlForExport);
$('#frmExcelExport').submit();
,而且效果很好.多谢Oleg !!!
and it is working very good.Thanks a bunch to Oleg!!!!
推荐答案
我认为如果使用HTTP POST
,则无法打开Excel.起作用的方法是使用HTTP GET
并对URL中所需的参数进行编码:
I think that you can't open Excel if you use HTTP POST
. The way which will work is to use HTTP GET
and encode parameters which you need in the URL:
window.location = "http://devmachine:9010/axis/StdPartSearchServlet?" +
$.param({someParamName: "someValue", anotherParam: 123});
相对于为application/vnd.ms-excel
注册的应用程序,Web浏览器将以Content-Disposition
标头中指定的PistonData.xls
的形式打开返回的数据(请参见Content-Type
标头).有关更多详细信息,请参见答案.
In the way the web browser will opens the returned data as PistonData.xls
specified in Content-Disposition
header with respect of the application registered for application/vnd.ms-excel
(see Content-Type
header). See the answer for more details.
如果需要防止不受控制地缓存从服务器返回的XLS数据,我建议您设置其他HTTP标头"Cache-Control: max-age=0"
或更好的"Cache-Control: private, max-age=0"
,以防止在不重新验证HTTP代理的情况下进行缓存.请参见此处,此处 a>有关其他信息.
If you need to prevent uncontrolled caching of the XLS data returned from the server I would recommend you to set additional HTTP header "Cache-Control: max-age=0"
or better "Cache-Control: private, max-age=0"
to prevent caching without re-validation on the HTTP proxy. See here, here for additional information.
这篇关于jQuery Excel导出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!