Save dynamically created table content in excel file using SheetJS

0 votes

I add rows into html table dynamically and I want to save the table content into xlsx file using SheetJs. The generated file is empty. Is somehow possible to do this in this case when table content was added this way? I also tried to add the rows rigth before creating the xlsx file..

<!DOCTYPE html>
<html>

<head>
    <script src="https://code.jquery.com/jquery-2.1.4.js"
        integrity="sha256-siFczlgw4jULnUICcdm9gjQPZkw/YPDqhQ9+nAOScE4=" crossorigin="anonymous"></script>
    <script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/amcharts/3.21.15/plugins/export/libs/FileSaver.js/FileSaver.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.6/xlsx.full.min.js"></script>

    <style>
        table,
        td {
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.3/xlsx.full.min.js">
    </script>

    <p>Click the button to add a new row at the first position of the
        table and then add cells and content</p>

    <table id="myTable">
        <TR>
        </TR>
    </table>
    <button type="button" id="first" onclick="First('myTable')">Principal</button>
    <button id="button-a">Create Excel</button>
    <script>
        function First(tableID) {
            let table = document.getElementById(tableID)
            table.innerHTML = "<tr>first</tr>";
        }

    </script>

    <script>
        var wb = XLSX.utils.table_to_book(document.getElementById('myTable'), { sheet: "Sheet JS" });
        var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'binary' });
        function s2ab(s) {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        }
        $("#button-a").click(function () {
            saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), 'test.xlsx');
        });
    </script>
</body>

</html>

 

Apr 4 in Database by gaurav
• 22,040 points
1,471 views

1 answer to this question.

0 votes
  1. text inside tr instead of td in dynamic content. This results in the table structure like below.

enter image description here

  1. XLSX.utils.table_to_book called before table content created.
answered Apr 4 by Edureka
• 13,640 points

Related Questions In Database

0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

Modify an existing Excel file using Openpyxl in Python

I am basically trying to copy some ...READ MORE

Mar 25 in Database by Edureka
• 13,640 points
1,102 views
0 votes
1 answer

Reading an Excel file in python using pandas

How to Use Pandas to Import an ...READ MORE

answered Mar 30 in Database by gaurav
• 22,040 points
878 views
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
812 views
0 votes
1 answer

How to rename a column in a database table using SQL?

For SQL Server, use sp_rename USE AdventureWorks; GO EXEC sp_rename 'Customers.CustomerTerritory.TerritoryID', ...READ MORE

answered Feb 9 in Database by Neha
• 8,940 points
146 views
0 votes
1 answer
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23 in Database by gaurav
• 22,040 points
292 views
0 votes
1 answer

How to download created excel file in node.js using exceljs

Download a sample Excel file using Node. ...READ MORE

answered Apr 4 in Database by Edureka
• 13,640 points
3,686 views
0 votes
1 answer

How to transform multiple tables in one excel sheet to one table with Power BI?

If my prediction is correct- You have a ...READ MORE

answered Apr 4 in Database by Edureka
• 13,640 points
895 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP