I was try to export an excel file using ExcelJS
Here is my console in VS Code terminal :
ERROR in node_modules/exceljs/
You can't write directly a file on client side. that method is mean to be used on backend side in nodejs. if your expectation is download this file on client side. your code should be like :-
import { Component } from "@angular/core";
import * as Excel from "exceljs";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title = "resttest10";
async downloadExcel() {
const date = new Date()
.toISOString()
.slice(0, 10)
.split("-")
.reverse()
.join("/");
console.log(date);
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet("My Sheet");
worksheet.columns = [
{ header: "Id", key: "id", width: 10 },
{ header: "Name", key: "name", width: 32 },
{ header: "D.O.B.", key: "dob", width: 15 }
];
worksheet.addRow({ id: 1, name: "John Doe", dob: new Date(1970, 1, 1) });
worksheet.addRow({ id: 2, name: "Jane Doe", dob: new Date(1965, 1, 7) });
workbook.xlsx.writeBuffer().then((data: any) => {
console.log("buffer");
const blob = new Blob([data], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
document.body.appendChild(a);
a.setAttribute("style", "display: none");
a.href = url;
a.download = "export.xlsx";
a.click();
window.URL.revokeObjectURL(url);
a.remove();
});
}
}
In your tsconfig.app.json file add "types": ["node"]