ExtJS:Grid数据导出至excel实例

页面导航:首页 > 网络编程 > JavaScript > ExtJS:Grid数据导出至excel实例

ExtJS:Grid数据导出至excel实例

来源: 作者: 时间:2016-02-04 09:15 【

导出函数ExportExcel()var config={ store: alldataStore, title: 测试标题 };var tab=tabPanel getActiveTab(); 当前活动状态的PanelExportExcel(tab,config); 调用导出函数ExportGridToExc

导出函数ExportExcel()

var config={ store: alldataStore,
	 title: '测试标题'
 };

var tab=tabPanel.getActiveTab();//当前活动状态的Panel

ExportExcel(tab,config);//调用导出函数

ExportGridToExcel.js

var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

};

Ext.override(Ext.grid.Panel,{
    getExcelXml: function(includeHidden, config) {
    	
        var worksheet = this.createWorksheet(includeHidden, config);
        //var totalWidth = this.getColumnModel().getTotalWidth(includeHidden);
        var innertitle = "';

        if (config && config.title) {
            innertitle = config.title;
        } else {
            innertitle = this.title;
        }
        return "' +
            '' +
            '' + innertitle + '' +
            '' +
                '' + worksheet.height + '' +
                '' + worksheet.width + '' +
                'False' +
                'False' +
            '' +
            '' +
                '' +
                    '' +
                    '' +
                    '' +
                        '' +
                        '' +
                        '' +
                        '' +
                    '' +
                    '' +
                    '' +
                    '' +
                '' +
                '' +
                    '' +
                    '' +
                    '' +
                    '' +
                '' +
                '' +
                    '' +
                    '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
        //                    '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
                '' +
                    '' +
                '' +
            '' +
            worksheet.xml +
            '';
    },
    createWorksheet: function(includeHidden, config) {
        // Calculate cell data types and extra class names which affect formatting
        var cellType = [];
        var cellTypeClass = [];
        //var cm = this.getColumnModel();
        var totalWidthInPixels = 0;
        var colXml = '';
        var headerXml = '';
        var visibleColumnCountReduction = 0;
        var innertitle = "';
        var innerstore = null;

        if (config && config.title) {
            innertitle = config.title;
        } else {
            innertitle = this.title;
        }
        if (!innertitle "| innertitle == '') {
            innertitle = "Main Title";
        }

        if (config && config.store) {
            innerstore = config.store;
        } else {
            innerstore = this.store;
        }
        for (var i=1;i< this.columns.length;i++) {
            if (includeHidden || !this.columns[i].isHidden()) {
                //debugger;
                var w = this.columns[i].getWidth();
                totalWidthInPixels += w;
                
                if ((this.columns[i].text === "") || (this.columns[i].getId() === "") ) {
                    cellType.push("None");
                    cellTypeClass.push("");
                    ++visibleColumnCountReduction;
                }
                else {
                    colXml += '';
                    headerXml += '' +
                        '' + this.columns[i].text + '' +
                        '';
                
                	var fld = innerstore.model.prototype.fields.items[i-1].type;
                    switch (fld.type) {
                        case "int":
                            cellType.push("Number");
                            cellTypeClass.push("int");
                            break;
                        case "float":
                            cellType.push("Number");
                            cellTypeClass.push("float");
                            break;
                        case "bool":
                        case "boolean":
                            cellType.push("String");
                            cellTypeClass.push("");
                            break;
                        case "date":
                            cellType.push("DateTime");
                            cellTypeClass.push("date");
                            break;
                        default:
                            cellType.push("String");
                            cellTypeClass.push("");
                            break;
                        
                    }
                    
                }
            }
        }
        var visibleColumnCount = cellType.length - visibleColumnCountReduction;

        var result = {
            height: 9000,
            width: Math.floor(totalWidthInPixels*30)+50
        };

        // Generate worksheet header details.
        var t = '' +
            '' +
                '' +
            '' +
            '' +
                colXml +
                '' +
                    '' +
                      '' +
                        '' + innertitle + '' +
                    '' +
                '' +
                '' +
                headerXml +
                '';

        // Generate the data rows from the data in the Store
        for (var i = 0, it = innerstore.data.items, l = it.length; i < l; i++) {
            t += '';
            var cellClass = (i & 1) ? 'odd' : 'even';
            r = it[i].data;
            var k = 0;
            for (var j=1;j< this.columns.length;j++) {
                if (includeHidden || !this.columns[j].isHidden()) {
                    //debugger;
                    var v = r[this.columns[j].dataIndex];
                    if (typeof this.columns[j].renderer == 'function' && cellType[k] != 'DateTime') {
                        var m = {};
                        v = this.columns[j].renderer(v, m, it[i], i, j, innerstore);
                        var re = /<[^>]+>/g;
                        if (v) {
                            v = v.toString().replace(re, '');
                        } else {
                            v = '';
                        }
                    }
                    if (cellType[k] !== "None") {

                        if (!v) {
                            t += '';
                        } else {
                            t += '';
                            if (cellType[k] == 'DateTime') {
                                t += v.format('Y-m-d\\TH:i:s.000'); // no space betwen  i: s
                            } else {
                                v = EncodeValue(v);

                                t += v;
                            }
                            t += '';
                        }
                    }
                    k++;
                }
            }
            t += '';
        }

        result.xml = t + '' +
            '' +
                '' +
                    '' +
                    '' +
                    '' +
                '' +
                '' +
                '' +
                    'Blank' +
                    '1' +
                    '32767' +
                    '' +
                    '600' +
                '' +
                '' +
                '' +
                'False' +
                'False' +
            '' +
        '';

        //Add function to encode value,2009-4-21
        function EncodeValue(v) {
            var re = /[\r|\n]/g; //Handler enter key
            v = v.toString().replace(re, '
');

            return v;
        };

        return result;
    }
  
});

function ExportExcel(gridPanel,config) {
    if (gridPanel) {	       
        var tmpExportContent = '';
        tmpExportContent=gridPanel.getExcelXml(true,config);     
            if (Ext.isIE || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3 ) {//在这几种中才需要
                var fd = Ext.get('frmDummy');
                if (!fd) {
                    fd = Ext.DomHelper.append(
                            Ext.getBody(), {
                                tag : 'form',
                                method : 'post',
                                id : 'frmDummy',
                                action : 'exportdata.',
                                target : '_blank',
                                name : 'frmDummy',
                                cls : 'x-hidden',
                                cn : [ {
                                    tag : 'input',
                                    name : 'exportContent',
                                    id : 'exportContent',
                                    type : 'hidden'
                                } ]
                            }, true);
                    
                }
                fd.child('#exportContent').set( {
                    value : tmpExportContent
                });
                fd.dom.submit();
            } else {
                document.location = 'data:application/vnd.ms-excel;base64,' + Base64.encode(tmpExportContent);
            }
    }
}


Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<