2013-02-22 72 views
1

我想將我的SDK2網格導出爲CSV或Excel--我懷疑此代碼可能存在於拉力賽某處,因爲您可以將用戶案例從組合項導出爲CSV。將SDK2網格導出爲CSV

是否有解決方案,人們都知道,可以發佈?

我在Sencha網站上發現了一些thread of this,它完全是客戶端,但我一直無法使用拉力賽SDK2網格使它工作。

我懷疑如果你只想去CSV,可能會有比較簡單的解決方案。

回答

5

查看了上面Charles提供的示例,並將此簡化對象導出爲EXT-JS網格(因此也適用於拉力網格)。

對於IE瀏覽器,直接進入Excel,其他瀏覽器會下載一個CSV--但使用Data URI方法(文件名不可控,Excel在打開之前抱怨一點)它有點不穩定,但它的功能。

如果有人真的知道Javascript想要發佈改進,我會很感激!

// Derived and simplified from example on bryntum.com 

Ext.define("GridExporter", { 
    dateFormat : 'Y-m-d g:i', 

    exportGrid: function(grid) { 
     if (Ext.isIE) { 
      this._ieToExcel(grid); 

     } else { 
      var data = this._getCSV(grid); 

      window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(data); 
     } 
    }, 

    _escapeForCSV: function(string) { 
     if (string.match(/,/)) { 
      if (!string.match(/"/)) { 
       string = '"' + string + '"'; 
      } else { 
       string = string.replace(/,/g, ''); // comma's and quotes-- sorry, just loose the commas 
      } 
     } 
     return string; 
    }, 

    _getFieldText: function(fieldData) { 
     var text; 

     if (fieldData == null || fieldData == undefined) { 
      text = ''; 

     } else if (fieldData._refObjectName && !fieldData.getMonth) { 
      text = fieldData._refObjectName; 

     } else if (fieldData instanceof Date) { 
      text = Ext.Date.format(fieldData, this.dateFormat); 

     } else if (!fieldData.match) { // not a string or object we recognize...bank it out 
      text = ''; 

     } else { 
      text = fieldData; 
     } 

     return text; 
    }, 

    _getFieldTextAndEscape: function(fieldData) { 
     var string = this._getFieldText(fieldData); 

     return this._escapeForCSV(string); 
    }, 

    _getCSV: function (grid) { 
     var cols = grid.columns; 
     var store = grid.store; 
     var data = ''; 

     var that = this; 
     Ext.Array.each(cols, function(col, index) { 
      if (col.hidden != true) { 
       data += that._getFieldTextAndEscape(col.text) + ','; 
      } 
     }); 
     data += "\n"; 

     store.each(function(record) { 
      var entry  = record.getData(); 
      Ext.Array.each(cols, function(col, index) { 
       if (col.hidden != true) { 
        var fieldName = col.dataIndex; 
        var text  = entry[fieldName]; 

        data += that._getFieldTextAndEscape(text) + ','; 
       } 
      }); 
      data += "\n"; 
     }); 

     return data; 
    }, 

    _ieGetGridData : function(grid, sheet) { 
     var that   = this; 
     var resourceItems = grid.store.data.items; 
     var cols   = grid.columns; 

     Ext.Array.each(cols, function(col, colIndex) { 
      if (col.hidden != true) { 
       console.log('header: ', col.text); 
       sheet.cells(1,colIndex + 1).value = col.text; 
      } 
     }); 

     var rowIndex = 2; 
     grid.store.each(function(record) { 
      var entry = record.getData(); 

      Ext.Array.each(cols, function(col, colIndex) { 
       if (col.hidden != true) { 
        var fieldName = col.dataIndex; 
        var text  = entry[fieldName]; 
        var value  = that._getFieldText(text); 

        sheet.cells(rowIndex, colIndex+1).value = value; 
       } 
      }); 
      rowIndex++; 
     }); 
    }, 

    _ieToExcel: function (grid) { 
     if (window.ActiveXObject){ 
      var xlApp, xlBook; 
      try { 
       xlApp = new ActiveXObject("Excel.Application"); 
       xlBook = xlApp.Workbooks.Add(); 
      } catch (e) { 
       Ext.Msg.alert('Error', 'For the export to work in IE, you have to enable a security setting called "Initialize and script ActiveX control not marked as safe" from Internet Options -> Security -> Custom level..."'); 
       return; 
      } 

      xlBook.worksheets("Sheet1").activate; 
      var XlSheet = xlBook.activeSheet; 
      xlApp.visible = true; 

      this._ieGetGridData(grid, XlSheet); 
      XlSheet.columns.autofit; 
     } 
    } 
}); 
+0

不錯。您還需要在_getFieldText中爲數字添加一個測試:like} else if(typeof(fieldData)===「number」){text =''+ fieldData; }否則如果(!fieldData.match){ //不是我們識別的字符串或對象...將其空白出來 text ='';其他{ } text = fieldData; } – curmudgeon 2013-03-07 18:51:47

1

您可以通過從底層商店獲取數據並創建Data URI來創建網格導出。您可能會遇到問題,以便在不同瀏覽器中正常工作。

您還可以看到Bryntum在此處製作的東西。