IE 读取execl文件信息

在这里插入代码片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="jquery-1.10.2.min.js"></script>
    <script>
        //解决 IE8 不支持console
        window.console = window.console || (function () {
            var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
                = c.clear = c.exception = c.trace = c.assert = function () { };
            return c;
        })();
        function uploads() {    
            $("#excel-file").click();
        }
 
        function Export() {
            var url = document.getElementById("excel-file").value;
            if (!url) { return; }
            var suffix = url.substring(url.lastIndexOf(".") + 1, url.length);
            if (!(suffix == "xls" || suffix == "xlsx")) {
                return alert("文件类型不正确");
            }
            importf(url);
        }
        function importf(url) {//导入
            // 获取 EXCEL json数据
            var jsondata = sheet_to_json(url);
            var column = [];
            var data = [column];
            for (var key in jsondata[0]) {//标题
                data[0].push(key);
            }
            for (var i = 0; i < jsondata.length; i++) {
                var row = [];
                data.push(row);
                for (var key in jsondata[i]) {
                    data[i + 1].push(jsondata[i][key]);
                }
            }
            //console.log(data);
            //便利Json对象
            var str;
            for (var key in data) {///IE8 不支持let写法,上面写了js中改为var   使用IE真是一种折磨(各种奇葩的问题)
                console.log(key + '---' + data[key])
                str = "<tr>";
                for (var key1 in data[key]) {///IE8 不支持let写法,上面写了js中改为var 
                    //console.log(key1 + '---' + data[key][key1])
                    if (data[key][key1] == "undefined") {
                        str += "<td> </td>";
                    } else {
                        str += "<td>" + data[key][key1] + "</td>";
                    }
 
                }
                str += "</tr>"
                $("#tab").append(str);
            }
        }
 
        function sheet_to_json(filePath) {
            var oXL = new ActiveXObject("Excel.Application");
            var oWB = oXL.Workbooks.open(filePath);
            oWB.worksheets(1).select();
            var oSheet = oWB.ActiveSheet;
            var jsondata = [];
            //debugger
            for (var row = 1; (oSheet.Cells(row, 1) + '') != 'undefined'; row++) {  //控制行数
 
                //for (var col = 1; (oSheet.Cells(row, col) + '') != 'undefined'; col++) {
                for (var col = 1; col <= 10; col++) {//列需要固定,防止中间为空、导致后面读取不到
                    if (row > 1) {
                        //console.log(oSheet.Cells(row, col).value);
                        jsondata[row - 2] += '"' + oSheet.Cells(1, col).value + '":' + '"' + oSheet.Cells(row, col).value + '",';
                    }
                }
                if (row > 1) {
                    jsondata[row - 2] = JSON.parse(('{' + jsondata[row - 2].substr(0, jsondata[row - 2].lastIndexOf(',')) + '}').replace('undefined', ''));
                }
            }
            oWB.close();
            oXL.Application.Quit();
            return jsondata;
        }
 
        /**********************************************json兼容IE8*****************************************************/
        //json不兼容的解决办法:
        if (!window.JSON) {
            window.JSON = {
                parse: function (jsonStr) {
                    return eval('(' + jsonStr + ')');
                },
                stringify: function (jsonObj) {
                    var result = '',
                        curVal;
                    if (jsonObj === null) {
                        return String(jsonObj);
                    }
                    switch (typeof jsonObj) {
                        case 'number':
                        case 'boolean':
                            return String(jsonObj);
                        case 'string':
                            return '"' + jsonObj + '"';
                        case 'undefined':
                        case 'function':
                            return undefined;
                    }
 
                    switch (Object.prototype.toString.call(jsonObj)) {
                        case '[object Array]':
                            result += '[';
                            for (var i = 0, len = jsonObj.length; i < len; i++) {
                                curVal = JSON.stringify(jsonObj[i]);
                                result += (curVal === undefined ? null : curVal) + ",";
                            }
                            if (result !== '[') {
                                result = result.slice(0, -1);
                            }
                            result += ']';
                            return result;
                        case '[object Date]':
                            return '"' + (jsonObj.toJSON ? jsonObj.toJSON() : jsonObj.toString()) + '"';
                        case '[object RegExp]':
                            return "{}";
                        case '[object Object]':
                            result += '{';
                            for (i in jsonObj) {
                                if (jsonObj.hasOwnProperty(i)) {
                                    curVal = JSON.stringify(jsonObj[i]);
                                    if (curVal !== undefined) {
                                        result += '"' + i + '":' + curVal + ',';
                                    }
                                }
                            }
                            if (result !== '{') {
                                result = result.slice(0, -1);
                            }
                            result += '}';
                            return result;
 
                        case '[object String]':
                            return '"' + jsonObj.toString() + '"';
                        case '[object Number]':
                        case '[object Boolean]':
                            return jsonObj.toString();
                    }
                }
            };
        }
        /**********************************************json兼容IE8*******************************************************/
    </script>
</head>
<body>
    <input type="file" id="excel-file" onchange="Export()" style="display:none" />
    <input type="button" value="上传excel"  id="tbn" onclick="uploads()"/>
    <table id="tab" border="1"></table>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值