简体   繁体   English

使用 Jquery 和 Z3EB7106C3477A60E6BBF5DBFDE1DA59 将 Excel 导入到 Html 表

[英]Import Excel to Html Table Using Jquery and Ajax

I am trying to import excel file to HTML Table using Jquery.我正在尝试使用 Jquery 将 excel 文件导入 HTML 表。 I have tried the code below.我已经尝试了下面的代码。 Do I have to do any other thing?我还需要做其他事情吗? The file uploaded is not showing in the table yet.上传的文件尚未显示在表格中。 Please assist.请协助。

Below is the Script下面是脚本

var ExcelToJSON = function () {

            this.parseExcel = function (file) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    var data = e.target.result;
                    var workbook = XLSX.read(data, {
                        type: 'binary'
                    });
                    workbook.SheetNames.forEach(function (sheetName) {
                        // Here is your object
                        var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                        var json_object = JSON.stringify(XL_row_object);
                        productList = JSON.parse(json_object);

                        var rows = $('#tblItems tbody tr',);
                        console.log(productList)
                        for (i = 0; i < productList.length; i++) {

                            var columns = Object.values(productList[i])
                            rows.eq(i).find('td').text(columns[0]);
                            rows.eq(i).find('td').text(columns[1]);
                            rows.eq(i).find('td').text(columns[2]);
                            rows.eq(i).find('td').text(columns[3]);
                            rows.eq(i).find('td').text(columns[4]);
                           
                        }

                    })
                };
                reader.onerror = function (ex) {
                    console.log(ex);
                };

                reader.readAsBinaryString(file);



            };
        };

        function handleFileSelect(evt) {

            var files = evt.target.files; // FileList object
            var xl2json = new ExcelToJSON();
            xl2json.parseExcel(files[0]);
        }

        document.getElementById('fileupload').addEventListener('change', handleFileSelect, false);

Below is the HTML下面是 HTML

<div class="w3-col w3-half">
                    <div class="form-group">
                       

                        <input id="fileupload" type=file name="files[]">
                    </div>
                </div>

<div class="w3-responsive">
                    @*<h2 class="w3-center"><strong>All Items in Stock </strong></h2>*@
                    <table id="tblItems" class="table w3-table-all w3-hoverable">
                        <thead>
                            <tr>

                                <th>Item ID</th>
                                <th>Item Name</th>
                                <th>Cost Price</th>
                                <th>Sales Price</th>
                                <th>Item Discontinued?</th>

                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>

After trying to upload an excel file, it does nothing and the excel file been imported does not display on the Table designated for it.尝试上传 excel 文件后,它什么也不做,导入的 excel 文件不会显示在为其指定的表上。

Kindly try this code请尝试此代码

 var ExcelToJSON = function() { this.parseExcel = function(file) { var reader = new FileReader(); reader.onload = function(e) { var data = e.target.result; var workbook = XLSX.read(data, { type: 'binary' }); workbook.SheetNames.forEach(function(sheetName) { var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]); var productList = JSON.parse(JSON.stringify(XL_row_object)); var rows = $('#tblItems tbody'); // console.log(productList) for (i = 0; i < productList.length; i++) { var columns = Object.values(productList[i]) rows.append(` <tr> <td>${columns[0]}</td> <td>${columns[1]}</td> <td>${columns[2]}</td> <td>${columns[3]}</td> <td>${columns[4]}</td> </tr> `); } }) }; reader.onerror = function(ex) { console.log(ex); }; reader.readAsBinaryString(file); }; }; function handleFileSelect(evt) { var files = evt.target.files; // FileList object var xl2json = new ExcelToJSON(); xl2json.parseExcel(files[0]); } document.getElementById('fileupload').addEventListener('change', handleFileSelect, false);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.0/dist/xlsx.full.min.js"></script> <div class="w3-col w3-half"> <div class="form-group"> <input id="fileupload" type=file name="files[]"> </div> </div> <div class="w3-responsive"> <h2 class="w3-center"><strong>All Items in Stock </strong></h2> <table id="tblItems" class="table w3-table-all w3-hoverable"> <thead> <tr> <th>Item ID</th> <th>Item Name</th> <th>Cost Price</th> <th>Sales Price</th> <th>Item Discontinued?</th> </tr> </thead> <tbody></tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM