2016-07-26 38 views
0

我想將extjs網格內容輸出爲excel文件。所以我做了什麼已經: 我送過Ext.Ajax.request電網的servlet的JSON內容,如從extjs grid輸出excel

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的我得到的JSON在servlet的做一些東西,例如創建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文件,但是當我打開它會出現錯誤,說格式或擴展名是無效的。

原因? 是否可能取決於編碼? 我做錯了什麼?

回答

0

分機配備了內置的Excel導出您可能需要使用,看看你得到想要的結果

grid.saveDocumentAs({ 
    type: 'excel', 
    title: 'My Excel Title', 
    fileName: 'MyExcelFileName.xml' 
}); 

從工具箱嘗試:Ext.grid.plugin.Exporter