JS 读取本地Excel文件

  首先我们先引用一个Excel的类库xlsx.full.min.js

中间处理:

'use strict';
var ExcelReader = {
    isFirstRead: true,
    fixdata: function (data) {
        var o = "",
            l = 0,
            w = 10240;
        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    },
    read: function (file, callback, sheetIndex) {
        var self = this;
        var isABS = false;
        if (this.isFirstRead) {
            this.extendFileReader();
        }
        if (!file) {
            return;
        }
        if (!sheetIndex) {
            sheetIndex = 0;
        }
        var f;
        if (file.rawFile) {
            f = file.rawFile;//使用Kendo Ui的上传控件
        }
        else {
            f = file;
        }
        var reader = new FileReader();
        reader.onload = function (e) {
            var data;
            var wb;
            if (!isABS) {
                data = e.target.result;
            }
            else {
                data = e;
            }
            if (!isABS && !data.slice) {
                isABS = true;
                reader.readAsBinaryString(f);
                return;
            }
            if (!isABS) {
                wb = XLSX.read(btoa(self.fixdata(data)), {
                    type: 'base64'
                });
            }
            else {
                wb = XLSX.read(btoa(data), {
                    type: 'base64'
                });
            }
            callback(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[sheetIndex]]));
        };
        reader.readAsArrayBuffer(f);
    },
    extendFileReader: function () {
        this.isFirstRead = false;
        if (!FileReader.prototype.readAsBinaryString) {
            FileReader.prototype.readAsBinaryString = function (fileData) {
                var binary = "";
                var self = this;
                var reader = new FileReader();
                reader.onload = function (e) {
                    var bytes = new Uint8Array(reader.result);
                    var length = bytes.byteLength;
                    for (var i = 0; i < length; i++) {
                        binary += String.fromCharCode(bytes[i]);
                    }
                    self.onload(binary);
                }
                reader.readAsArrayBuffer(fileData);
            }
        }
    }
}

  

 

使用方式:

$("#ImportCtrl").kendoUpload({
                    select: function (e) {
                        $.each(e.files, function (index, value) {
                            ExcelReader.read(value, importFilesSuccess);
                        });
                    }
                }).data("kendoUpload");

var importFilesSuccess = function (d) {
//d:JSON DATA
}

  

 

转载于:https://www.cnblogs.com/magic_evan/p/8404359.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值