I am currently working on exporting an HTML table to Excel using JavaScript. I have encountered an issue where it does not export to the newer version of Excel with the xlsx
extension.
However, it works fine and exports to older versions of Excel with the xls
extension.
When attempting to export from the HTML table to Excel with the new xlsx
extension, I receive an error message:
cannot open file `.xlsx` because the file extension is not valid or the file is corrupted.
So, how can I solve this problem in order to work with the xlsx
extension and open the newer version of Excel?
The function below is used to get selected civil IDs:
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></tr>";
$("input[name='statusCheckbox']:checked").each(function () {
var row = $(this).closest("tr");
var cells = row.find("td");
table += "<tr>";
cells.each(function () {
table += "<td>" + $(this).text() + "</td>";
});
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";
link.click();
}
}
When debugging the variable var table
, it provides me with the HTML table script as shown in the fiddle linked below. How can I export this to Excel with the xlsx
format?
https://jsfiddle.net/36s1qhov
How can I modify the following section to export to Excel as a xlsx
sheet:
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";
link.click();