将SDK2网格导出为CSV

时间:2013-02-22 17:03:57

标签: extjs extjs4 rally

我想将我的SDK2网格导出为CSV或Excel--我怀疑此代码可能存在于Rally某处,因为您可以将用户故事从项目组合项目导出为CSV。

人们都知道并且可以发布这个解决方案吗?

我在Sencha网站上发现了一些thread of this,它完全是客户端的,但是我无法使用Rally SDK2网格来使用它。

我怀疑如果您只想转到CSV,可能会有更简单的解决方案。

2 个答案:

答案 0 :(得分:5)

查看上面Charles给出的示例,并将此简化对象导出为EXT-JS网格(因此也适用于Rally Grid)。

对于IE,直接进入Excel,对于其他浏览器下载CSV--但它使用数据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; 
        }
    }
});

答案 1 :(得分:1)

您可以通过从基础商店获取数据并创建Data URI来从网格创建导出。您可能会遇到问题,以便在不同的浏览器中正常工作。

您还可以看到Bryntum在此处制作的内容。