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