自己临时修改别人的SheetJS调用方法

自己在别人基础上,临时修改的代码,

<html>
<head>

    <title>JS-XLSX Live Demo</title>

    <style>
        #drop {
            border: 2px dashed #bbb;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            border-radius: 5px;
            padding: 25px;
            text-align: center;
            font: 20pt bold,"Vollkorn";
            color: #bbb
        }

        #b64data {
            width: 100%;
        }

        a {
            text-decoration: none
        }

        button {
        }
    </style>

    <script type="text/javascript" src="dist/xlsx.full.min.js"></script>
    <script type="text/javascript" src="dist/xlsx.core.min.js"></script>

    <script type="text/javascript" src="dist/jquery.min.js"></script>

</head>

    <body>
<pre>
<input type="file" name="xlfile" id="xlf" onchange="importf(this)">
</pre>
<pre id="out"></pre>
        <br>
        <div id="demo"></div>

        <script>
            /*
                  FileReader共有4种读取方法:
                  1.readAsArrayBuffer(file):将文件读取为ArrayBuffer。
                  2.readAsBinaryString(file):将文件读取为二进制字符串
                  3.readAsDataURL(file):将文件读取为Data URL
                  4.readAsText(file, [encoding]):将文件读取为文本,encoding缺省值为'UTF-8'
                               */
            var workbook;//读取完成的数据
            var rABS = false; //是否将文件读取为二进制字符串

            function importf(obj) {//导入
                debugger;
                if (!obj.files) {
                    return;
                }
                var f = obj.files[0];
                var reader = new FileReader();
                reader.onload = function (e) {
                    var data = e.target.result;
                    if (rABS) {
                        workbook = XLSX.read(btoa(fixdata(data)), {//手动转化
                            type: 'base64'
                        });
                    } else {
                        workbook = XLSX.read(data, {
                            type: 'binary'
                        });
                    }
                    // 循环页签取数
                    console.log("页签数量:" + workbook.SheetNames.length);
                    var showHtml = "";
                    for (var sheetNum = 0; sheetNum < Number(workbook.SheetNames.length); sheetNum++) {
                        var first_sheet_name = workbook.SheetNames[sheetNum]; // 获取工作簿中的工作表名字
                        console.log("workbook.SheetNames[0]" + first_sheet_name)
                        var address_of_cell = 'B1'; // 提供一个引用样式(单元格下标)
                        var address_of_cell2 = 'B2'; // 提供一个引用样式(单元格下标)

                        var worksheet = workbook.Sheets[first_sheet_name]; // 获取对应的工作表对象
                        //				console.log(worksheet['!ref']) // 获得该excel的内容范围 例如: A1:F30
                        var scope = worksheet['!ref'];
                        if (scope == undefined) {
                            console.log("文件格式有误!")
                            showHtml = showHtml + first_sheet_name + "页签解析有误,请检查格式是否正确! <br />"
                            break;
                        } else {
                            showHtml = showHtml + first_sheet_name + "页签已解析完成 <br />"
                        }
                        // js截取某个字符串后面的内容:
                        scope = scope.match(/:(\S*)/)[1]; // 获取表格边界 例如:F30
                        console.log(scope);
                        var reg = /[A-Z]/g;
                        char_scope = scope.match(reg); //
                        console.log("===========" + char_scope);

                        // 用正则去除字符串种的数字,获取表格边界字母值  例如:F
                        var scopeLetter = scope.replace(/\d+/g, '');
                        console.log("=====ssss======" +scopeLetter);
                        // 用正则将非数字的去除,获取表格边界行值  例如:30
                        var scopeNum = scope.replace(/[^0-9]/ig, "");
                        //			console.log(Number(scopeNum)+1);
                        // 表名称
                        var tableName = "";
                        // 表注释
                        var tableNameNote = "";
                        // 创建表结构的字符串
                        var tableSql = "create table ";
                        // 创建表字段注释字符串
                        var tableNote = "";
                        // 创建表的主键字符串
                        var tablePk = "";
                        //				// 循环获取数据
                        for (var num = 1; num < Number(scopeNum) + 1; num++) { //  循环边界行值
                            var isNull = "";// 是否为空
                            var def = ""; // 默认值
                            for (var i = 0; i < 26; i++) {// 循环边界字母值
                                var letters = String.fromCharCode(65 + i); // 字母
                                console.log(String.fromCharCode(65+i)+num);//输出A-Z  26个大写字母
                                var address_cell = String.fromCharCode(65 + i) + num; // 提供一个引用样式(单元格下标)
                                var desired_cell = worksheet[address_cell];// 获取对应的单元格对象
                                var desired_value = (desired_cell ? desired_cell.v : undefined);// 获取对应的单元格对象的值
                                console.log(desired_value)
                                // 表结构字符串
                               
                            }
                        }
                        console.log(tableSql);
                        console.log(tableNote);
                        console.log(tablePk);
                        //function download(filename, text) {
                        //    var element = document.createElement('a');
                        //    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
                        //    element.setAttribute('download', filename);

                        //    element.style.display = 'none';
                        //    document.body.appendChild(element);

                        //    element.click();

                        //    document.body.removeChild(element);
                        //}
                        //download(first_sheet_name + ".sql", tableSql + "\n" + tableNote + "\n" + tablePk);

                    }

                    document.getElementById("demo").innerHTML = showHtml;
                };
                if (rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            }

            function fixdata(data) { //文件流转BinaryString
                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;
            }
        </script>


    </body>
</html>

  

 

posted @ 2022-01-21 12:03  苦行者的刀  阅读(81)  评论(0编辑  收藏  举报