1

我正在使用框架 JavaScript ExtJS 版本 3.2.1。从我的数据网格中,我想将数据导出到 Excel 文件。一切正常,除非网格有 1 个或多个复选框列:在这种情况下,我有一条错误消息:

p is null

如果我隐藏包含复选框的列,我可以再次导出。这是我定义复选框列时的代码快照(我构建了一个框架,因为我使用了很多网格视图):

this.column_model_sample_confirmed = new Ext.grid.CheckColumn({
        id:         cmnSampleConfirmed,
        header:         cmhSampleConfirmed,
        dataIndex:      cmnSampleConfirmed,
        align:          _KMX_EXT_CSS_LEFT_,
        width:          30,
        sortable:       true,
        hidden:         false, // sample_confirmed
        resizable:      true,
        filter:         {},
        editor:         new Ext.form.TextField({}),
        editable:       false,
        menuDisabled:   false,
        hideable:       true,
        fixe:           true
    });

不要注意我使用的变量/常量,因为这段代码可以正常工作。现在,实现我的按钮“导出到 Excel”的代码:

   /**
    * Export the grid in Excel
    * FIXED: To get more compatibility with major browsers
    * @param {Object}   my_grid         an editor grid panel instance
    * @param {String}   my_url_export   the url to get the data exported (ajax call)
    * @return NEANT
    */
    var _kmx_fct_exportExcel = function(my_grid, my_url_export){
        // we have to change the name of the worksheet to avoid some forbidden caracters such "(" or ":" to be able to open it with EXCEL
        var tmp = my_grid.title;
        try {
            my_grid.setTitle('Sheet1');
            var exportContent = my_grid.getExcelXml();
                    alert('THIS ALERT IS NOT DISPLAYED!');
            if (!Ext.isIE6 && !Ext.isIE7 && !Ext.isSafari && !Ext.isSafari2 && !Ext.isSafari3) {
                //alert('Ext.isIE6 = ' + Ext.isIE6 + "\n" + 'Ext.isIE7 = ' + Ext.isIE7 + "\n" + 'Ext.isSafari = ' + Ext.isSafari + "\n" + 'Ext.isSafari2 = ' + Ext.isSafari2 + "\n" + 'Ext.isSafari3 = ' + Ext.isSafari3);
                document.location='data:application/vnd.ms-excel;base64,' + Base64.encode(exportContent);
            }
            else {
                if (!Ext.fly('frmDummy')) {
                    var frm = document.createElement('form');
                    frm.id = 'frmDummy';
                    frm.name = id;
                    frm.className = 'x-hidden';
                    document.body.appendChild(frm);
                }
                Ext.Ajax.request({
                    url: my_url_export,
                    method : _self.kmx_form_method_post,
                    form: 
                        Ext.fly('frmDummy'),
                        callback: function(o, s, r){
                            alert(r.responseText);
                            //alert('_kmx_fct_exportExcel callback function: OK');
                        },
                    isUpload: true,
                    params: { ex: exportContent }
                });
            }
        }
        catch (err) {
            txt="There was an error on trying to export the data.\n\n";
            txt+="Error message: " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
            alert(txt);
        }
        my_grid.setTitle(tmp); // restore the grid panel name
    };

这个私有函数是在一个网格面板“类”中为曾经使用 ExtJS 3 的人定义的。同样,不要为我使用的变量/常量付费。问题似乎是这条线:

var exportContent = my_grid.getExcelXml();

因为此代码之后的警报消息:

alert('THIS ALERT IS NOT DISPLAYED!');

不显示。 所以,我的问题是:当你有一个复选框列时,如何将数据导出为 Excel 格式?

4

0 回答 0