2
Answers

why JavaScript code export to excel working with old office extension

Photo of ahmed salah

ahmed salah

May 16
483
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 .

code for html and javascript that not export to excel new version xlsx

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

html table generated by var table from debug 

Answers (2)

2
Photo of Abhishek Chadha
264 7.3k 517.6k May 17

To export HTML table data to an Excel file with the .xlsx extension (compatible with newer versions of Excel), you'll need to use a JavaScript library that supports creating Excel files with that format. One popular library for this purpose is SheetJS. Here's how you can modify your code to use SheetJS for exporting:

  1. First, include the SheetJS library in your HTML file. You can either download it or include it from a CDN. Here's how to include it from a CDN:
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.4/xlsx.full.min.js"></script>
  1. Modify your JavaScript code to use SheetJS for exporting the table data to an Excel file with the .xlsx extension:
 
function exportToExcel() { var wb = XLSX.utils.table_to_book(document.getElementById('your-table-id')); XLSX.writeFile(wb, 'filename.xlsx'); }

Replace 'your-table-id' with the ID of your HTML table, and 'filename.xlsx' with the desired name for the Excel file.

With this code, SheetJS will convert your HTML table to an Excel file with the .xlsx extension, which should be compatible with newer versions of Excel.

1
Photo of ahmed salah
991 689 52k May 17

I need to allow sheetjs on function below to export html table to  excel xlsx using javascript 

i need to export var table to excel using sheet js so how to do that please

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();
    }
}`