There are multiple ways to achieve this, but one possible solution is:
$scope.tableData = {
title: "My Table",
rows: [
{col1: "Row 1 Column 1", col2: "Row 1 Column 2"},
{col1: "Row 2 Column 1", col2: "Row 2 Column 2"},
{col1: "", col2: ""}, // empty row
{col1: "", col2: ""} // empty row
]
};
npm install xlsx --save
$scope.exportToExcel = function() {
var wb = XLSX.utils.book_new();
var ws = XLSX.utils.json_to_sheet([$scope.tableData.title], {header:[""]}); // title row
XLSX.utils.sheet_add_json(ws, $scope.tableData.rows, {skipHeader:true, origin:"A2"}); // table rows
wb.Sheets["Sheet1"] = ws;
var wbout = XLSX.write(wb, {bookType:"xlsx", type:"binary"});
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "mytable.xlsx"); // save as file
};
<button ng-click="exportToExcel()">Export to Excel</button>
Asked: 2022-09-14 11:00:00 +0000
Seen: 16 times
Last updated: Mar 30 '22