Best way to read Excel file and convert to JSON data (but error facing)

1

i tried to convert uploaded excel file to JSON model data using client side model not server side, but i'm unable to convert excel file to JSON data bcz.

0x800a138f - JavaScript runtime error: Unable to get property 'SheetNames' of undefined or null reference.

here is that url:

http://oss.sheetjs.com/js-xlsx/

expecting O/P:

{
      "Sheet1": [
        {
          "BookID": "56353",
          "BookAuthor": "Sir Alex Ferguson",
          "BookTitle": "Leading"
        },
        {
          "BookID": "73638",
          "BookAuthor": "Eric Smith",
          "BookTitle": "How Google Works"
        },
        {
          "BookID": "37364",
          "BookAuthor": "William Shakespeare",
          "BookTitle": "The Merchant of Venice"
        }
      ]
    }

Here is my Code:

<script src="shim.js"></script>
<script src="xlsx.full.min.js"></script>
<script>
            var X = XLSX;
            var XW = {
                msg: 'xlsx',
                worker: './xlsxworker.js'
            };
            var global_wb, sheetName;
            var process_wb = (function () {
                var OUT = document.getElementById('out');
                var HTMLOUT = document.getElementById('htmlout');

                var get_format = (function () {
                    var radios = document.getElementsByName("format");
                    return function () {
                        for (var i = 0; i < radios.length; ++i) if (radios[i].checked || radios.length === 1) return radios[i].value;
                    };
                })();

                var to_json = function to_json(workbook) {
                    var result = {};
                    workbook.SheetNames.forEach(function (sheetName) {
                        var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
                        if (roa.length) result[sheetName] = roa;
                    });
                    return JSON.stringify(result, 2, 2);
                };

                return function process_wb(wb) {
                    global_wb = wb;
                    var output = "";
                    switch (get_format()) {
                        case "json": output = to_json(wb); break;
                    }
                    if (OUT.innerText === undefined) OUT.textContent = output;
                    else OUT.innerText = output;
                    if (typeof console !== 'undefined') console.log("output", new Date());
                };
            })();

            var setfmt = window.setfmt = function setfmt() { if (global_wb) process_wb(global_wb); };

            var b64it = window.b64it = (function () {
                var tarea = document.getElementById('b64data');
                return function b64it() {
                    if (typeof console !== 'undefined') console.log("onload", new Date());
                    var wb = X.read(tarea.value, { type: 'base64', WTF: false });
                    process_wb(wb);
                };
            })();

            var do_file = (function () {
                var rABS = typeof FileReader !== "undefined" && (FileReader.prototype || {}).readAsBinaryString;
                var domrabs = document.getElementsByName("userabs")[0];
                if (!rABS) domrabs.disabled = !(domrabs.checked = false);

                var use_worker = typeof Worker !== 'undefined';
                var domwork = document.getElementsByName("useworker")[0];
                if (!use_worker) domwork.disabled = !(domwork.checked = false);

                var xw = function xw(data, cb) {
                    var worker = new Worker(XW.worker);
                    worker.onmessage = function (e) {
                        switch (e.data.t) {
                            case 'ready': break;
                            case 'e': console.error(e.data.d); break;
                            case XW.msg: cb(JSON.parse(e.data.d)); break;
                        }
                    };
                    worker.postMessage({ d: data, b: rABS ? 'binary' : 'array' });
                };

                return function do_file(files) {
                    rABS = domrabs.checked;
                    use_worker = domwork.checked;
                    var f = files[0];
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        if (typeof console !== 'undefined') console.log("onload", new Date(), rABS, use_worker);
                        var data = e.target.result;
                        if (!rABS) data = new Uint8Array(data);
                        if (use_worker) xw(data, process_wb);
                        else process_wb(X.read(data, { type: rABS ? 'binary' : 'array' }));
                    };
                    if (rABS) reader.readAsBinaryString(f);
                    else reader.readAsArrayBuffer(f);
                };
            })();

            (function () {
                var drop = document.getElementById('drop');
                if (!drop.addEventListener) return;

                function handleDrop(e) {
                    e.stopPropagation();
                    e.preventDefault();
                    do_file(e.dataTransfer.files);
                }

                function handleDragover(e) {
                    e.stopPropagation();
                    e.preventDefault();
                    e.dataTransfer.dropEffect = 'copy';
                }

                drop.addEventListener('dragenter', handleDragover, false);
                drop.addEventListener('dragover', handleDragover, false);
                drop.addEventListener('drop', handleDrop, false);
            })();

            (function () {
                var xlf = document.getElementById('xlf');
                if (!xlf.addEventListener) return;
                function handleFile(e) { do_file(e.target.files); }
                xlf.addEventListener('change', handleFile, false);
            })();
        </script>
jquery
xlsx
xls
asked on Stack Overflow Sep 27, 2017 by ethiraj

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0