✔ HTML 5 ⚡ How to read excel file with JavaScript 🔥 Tecq Mate Tutorials ✌

How to read excel file with HTML 5 JavaScript?

<input type="file" onchange="process(this.files[0])"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/jszip.js" type="text/javascript"></script>
<script type="text/javascript">
    function process(file) {
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/i;
        if (regex.test(file.name)) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                 if (reader.readAsBinaryString) {
                    reader.onload = function (e) {
                        loadData(e.target.result);
                    };
                    reader.readAsBinaryString(file);
                } else {
                    reader.onload = function (e) {
                        var data = "";
                        var bytes = new Uint8Array(e.target.result);
                        for (var i = 0; i < bytes.byteLength; i++) {
                            data += String.fromCharCode(bytes[i]);
                        }
                        loadData(data);
                    };
                    reader.readAsArrayBuffer(file);
                }
            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid Excel file.");
        }
    };
    function loadData(data) {
        if(!data){
          alert('No data found');
          return;
        }
        alert('check browser console');
        var workbook = XLSX.read(data, {
            type: 'binary'
        });
        workbook.SheetNames.forEach(function(sheetName) {
           console.log(sheetName,workbook.Sheets[sheetName],XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]),XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]));
      });
    };
</script>

Comments

Popular posts from this blog