【发布时间】:2016-11-30 03:02:43
【问题描述】:
我想将 extjs 网格内容导出到 excel 文件。所以我已经做了什么: 我通过Ext.Ajax.request发送到网格的servlet json内容,比如
Ext.Ajax.request({
url : 'ExportToExcel',
method:'POST',
jsonData: this.store.proxy.reader.rawData,
scope : this,
success : function(response,options) {
this.onExportSuccess(response, options);
},
//method to call when the request is a failure
failure: function(response, options){
alert("FAILURE: " + response.statusText);
}
});
然后在 servlet 中我得到 servlet 中的 json 做一些事情,例如创建excel文件,将其转换为字节数组并尝试响应。 在 Ext.Ajax.request 在成功方法中,当我尝试做这样的事情时:
var blob = new Blob([response.responseText], { type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet});
var downloadUrl = window.URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
它下载excel文件,但是当我打开它时出现错误,说格式或扩展名无效。
为什么? 是否可能取决于编码? 我做错了什么?
【问题讨论】: