-1

I working on export html table to excel using JAVASCRIPT . I face issue it not export to excel office New version with extension xlsx .

but it working fine and export to old version office excel with extension xls .

when export from html table to excel new version with xlsx extension it give me error

cannot open file `.xlsx` because file extension is not valid or file corrupted .

so How to solve this issue to working with xlsx extension and open new version excel .

`function getselected_civilid() {
    debugger
    var selectedCivilIds = [];
    $("input[name='statusCheckbox']:checked").each(function () {
        selectedCivilIds.push($(this).val());
    });

    if (selectedCivilIds.length > 0) {
        // Create an HTML table with headers
        var table = "<table><tr><th>م</th><th>رقم السجل التجاري</th><th>رقم الترخيص</th><th>رقم الجهه المدني</th><th>الكيان القانوني</th><th>المحافظة</th><th>القطاع</th><th>كود النشاط الدولى</th><th>اسم المنشأة</th><th>العنوان</th><th>الرقم الالي للعنوان</th><th>المدينة</th><th>رقم التليفون</th><th>البريد الالكترونى</th></tr>";

     
        $("input[name='statusCheckbox']:checked").each(function () {
            var row = $(this).closest("tr"); // Get the parent row of the checkbox
            var cells = row.find("td"); // Get all the cells in the row

            table += "<tr>";
            cells.each(function () {
                table += "<td>" + $(this).text() + "</td>"; // Add each cell value to the table
            });
            table += "</tr>";
        });

        table += "</table>";

     
        var blob = new Blob([table], {
            type: "application/vnd.ms-excel;charset=utf-8"
        });
 
        var link = document.createElement("a");
        link.href = URL.createObjectURL(blob);
        link.download = "LastData.xls"; // Set the filename for the downloaded file
        link.click();
    }
}`

when debug var table it give me html table script as fiddle below so How to export to excel xlsx New version

https://jsfiddle.net/36s1qhov

How to replace part below with sheet jz to export to excel xlsx

var blob = new Blob([table], {
            type: "application/vnd.ms-excel;charset=utf-8"
        });
 
        var link = document.createElement("a");
        link.href = URL.createObjectURL(blob);
        link.download = "LastData.xls"; // Set the filename for the downloaded file
        link.click();
5
  • 1
    Does this answer your question? How to export an HTML table as a .xlsx file Commented May 16, 2024 at 21:28
  • so how to install this on my project TableExport by clarketm i work on javascript on visual studio 2019 Commented May 16, 2024 at 21:41
  • You should not ask Stackoverflow how to use a Javascript library, that is something you should research on your own. There is also plenty of documentation on how to use it. github.com/clarketm/TableExport Commented May 16, 2024 at 22:29
  • can you please help me i need cdn first for table export then how to use it inside javascript function i added Commented May 16, 2024 at 22:46
  • @AndyRay same could be said for anything, including "javascript" itself. There are plenty of libraries authors that actually suggest asking on SO about their library. Commented May 17, 2024 at 9:38

1 Answer 1

0

"xlsx" is not just a file extension, its actually a format of a zip package that contains XML and other files. you can't just name the file "something.xlsx", you need to package it.
use existing library like https://www.npmjs.com/package/xlsx for this.

Sign up to request clarification or add additional context in comments.

1 Comment

how to install it on sheetjs or use it on JavaScript link you send support type script not javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.