Trying to implement Excel sheet to generate from Array. Using below Code it shows buffer.isbuffer is not function.
Same code works perfectly fine when using separately outside the project.
Can someone please help on this issue? Thanks in advance
ExcelService.ts
@Injectable()
export class ExcelService {
constructor() {
}
static toExportFileName(excelFileName: string): string {
return `${excelFileName}_export_${new Date().getTime()}.xlsx`;
}
public exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
const workbook: XLSX.WorkBook = {Sheets: {'data': worksheet}, SheetNames: ['data']};
XLSX.writeFile(workbook, ExcelService.toExportFileName(excelFileName));
}
}
app.component.ts
exportToExcel(event) {
this.excelService.exportAsExcelFile(PERSONS, 'persons');
}
app.component.html
<button (click)="exportToExcel()" class="btn btn-primary">Export to excel</button>
model.ts
id: number;
name: String;
surname: String;
age: number;
}
export const PERSONS: Person[] = [
{
id: 1,
name: 'Thomas',
surname: 'Novicky',
age: 21
},
{
id: 2,
name: 'Adam',
surname: 'Tracz',
age: 12
},
{
id: 3,
name: 'Steve',
surname: 'Laski',
age: 38
}
];
Console Error
VM1526 CommentTableComponent.ngfactory.js:453 ERROR TypeError: Buffer.isBuffer is not a function
at Function.module.exports.test (VM1423 vendor.js:182405)
at Object.exports.getTypeOf (VM1423 vendor.js:183869)
at Object.exports.transformTo (VM1423 vendor.js:183851)
at JSZipSync.utf8encode (VM1423 vendor.js:183183)
at JSZipSync.generate (VM1423 vendor.js:183085)
at write_zip_type (VM1423 vendor.js:210416)
at writeSync (VM1423 vendor.js:210506)
at Object.writeFileSync (VM1423 vendor.js:210529)
at ExcelService.push../src/app/shared/services/files/excel.service.ts.ExcelService.exportAsExcelFile (VM1424 main.js:6982)
at CommentTableComponent.push../src/app/shared/containers/comment-table/comment-table.component.ts.CommentTableComponent.exportToExcel (VM1424 main.js:4847)```