JS解析excel中的手机号码

添加依赖

js-xlsx
下载dist目录下的xlsx.full.min.js文件

代码

<html>

<head>
    <script src="./xlsx.full.min.js"></script>
</head>

<body>
    <input id="file" type="file" />
    <script>
        var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
        var rABS = typeof FileReader !== 'undefined' && FileReader.prototype && FileReader.prototype.readAsBinaryString;
        var file = document.getElementById("file");
        function to_json(workbook) {
            if (workbook.SSF) XLSX.SSF.load_table(workbook.SSF);
            var result = {};
            workbook.SheetNames.forEach(function (sheetName) {
                var roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1 });
                if (roa.length > 0) result[sheetName] = roa;
            });
            return result;
        }
        function filechange(e) {
            var file = e.target.files[0];
            var reader = new FileReader();
            reader.onload = function (le) {
                var data = le.target.result;
                var wb, arr;
                wb = XLSX.read(data, { type: 'binary' });
                var sheet = wb.SheetNames[0];
                var json = to_json(wb)[sheet];
                var phones = [];
                for(var i of json) {
                    for(var j of i){
                        if (j && phoneReg.test(j)) {
                            phones.push(j);
                        }
                    }
                }
                console.log(phones);
            }
            reader.readAsBinaryString(file);
        }
        file.addEventListener("change", filechange, false);
    </script>
</body>

</html>
JS解析excel中的手机号码
分享