面向对象纯JS 导出Excel,拥有它,你所需要做的就是setup数据格式(目前仅提供单个Sheet的导出,持续更新中...)。

var Excel = function(){
    this.initConfig = {
        name: '',
        sheetName: '',
        data : null
    }


    try{
        var args = arguments[0];
        for(var i in args) {
              if(this.initConfig.hasOwnProperty(i)) {
                  this.initConfig[i] = args[i];
              }
        }
        if(window.ActiveXObject) {
            this.xlApp = new ActiveXObject("Excel.Application");
             this.initIE();
        } else {
            if(!this.uri) {
               this.uri = 'data:application/vnd.ms-excel;base64,';
            }
            if(!this.template){
                this.template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>';
            }
            this.initNotIE();
        }


    } catch(e) {
        throw e;
    }
}


Excel.prototype.initIE = function() {
   this.xlBook = this.xlApp.Workbooks.Add;
    this.xlSheet = this.xlBook.Worksheets(1);
    this.xlSheet.name = this.initConfig.sheetName;
    this.xlSheet = this.xlBook.ActiveSheet;
    this.xlSheet.visible = true;


    var title = this.initConfig.data.title;
    var content = this.initConfig.data.content;
    if(title) {
        for(var i = 0, len = title.length; i < len; i++) {
            this.xlSheet.Cells(1, i).Value = title[i];
        }
    }
    if(content) {
        var row = 1;
        for(var i = 0, len = content.length; i < len; i++) {
            var item = content[i];
            row++;
            for(var j = 0, size = item.length; j < size; j++){
                this.xlSheet.Cells(row, j).Value = item[j];
            }
        }
    }


    this.save();
    this.destroy();
};


Excel.prototype.initNotIE = function(){
    var td = '<td>{value}</td>'
    var tr = '<tr>{value}</tr>';
    var title = this.initConfig.data.title;
    var content = this.initConfig.data.content;
    var html = '';
     if(title) {
        var headers = [];
        for(var i = 0, len = title.length; i < len; i++) {
             headers.push(this.format(td, {value:!title[i]?'':title[i]}));
        }
        var header = this.format(tr, {value: headers.join('')});
         html += header;
     }


     if(content) {
        for(var i = 0, len = content.length; i < len; i++) {
            var item = content[i];
            var contents = [];
            for(var j = 0, size = item.length; j < size; j++){
                 contents.push(this.format(td, {value: !item[j]?'':item[j]}));
            }
            html += this.format(tr, {value: contents.join('')});
        }
    }


    var name = this.initConfig.sheetName;
     var ctx = {worksheet: name, table: html};
    window.location.href = this.uri + this.encode64(this.format(this.template, ctx));
}
Excel.prototype.encode64 = function(s) {
     return window.btoa(unescape(encodeURIComponent(s)));
}


Excel.prototype.format = function(str, array) {
    return str.replace(/{(\w+)}/g, function(m, p) { return array[p]; })
}


Excel.prototype.save = function(){
    if(this.xlApp) {
        this.xlApp.Save();
    } else {


    }
}


Excel.prototype.destroy = function() {
     if(this.xlApp) {
         this.xlApp.UserControl = true
         this.xlApp.Quit();
         this.xlApp = null;

     }



使用简介:

new Excel({
                        name: 'Demo for yourself',
                        sheetName: 'Demo for yourself',
                        data : {
                            title: ['Column1','Column2','Column3'],
                            content: ['Value1','Value2','Value3']
                        }
                    });


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值