blobType: string = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
cols = ['Column1', 'Column2', 'Column3', 'Column4', 'Column5']
data = [
{ col1: "a1", col2: "b1", col3: "c1", col4: "d1", col5: "e1" },
{ col1: "a2", col2: "b2", col3: "c2", col4: "d2", col5: "e2" },
{ col1: "a3", col2: "b3", col3: "c3", col4: "d3", col5: "e3" },
{ col1: "a4", col2: "b4", col3: "c4", col4: "d4", col5: "e4" },
{ col1: "a5", col2: "b5", col3: "c5", col4: "d5", col5: "e5" }
]
This is my method
exportToExcel() {
var workbook = new Excel.Workbook();
workbook.creator = 'Web';
workbook.lastModifiedBy = 'Web';
workbook.created = new Date();
workbook.modified = new Date();
workbook.addWorksheet(this.sName, { views: [{state: 'frozen', ySplit: 3,
xSplit: 2, activeCell: 'A1', showGridLines: false}] });
let sheet = workbook.getWorksheet(1);
let head1 = ['Exported Data'];
sheet.addRow(head1);
sheet.addRow('');
sheet.getRow(3).values = this.cols;
sheet.columns = [
{ key: 'col1' },
{ key: 'col2' },
{ key: 'col3' },
{ key: 'col4' },
{ key: 'col5' }
];
sheet.addRows(this.data);
workbook.xlsx.writeBuffer().then(data => {
console.log(data);
const blob = new Blob([data], { type: this.blobType });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = this.excelFileName;
a.click();
})
}
}
Error:
Type 'Buffer' is not assignable to type 'BlobPart'. Type 'Buffer' is not assignable to type 'Blob'. Property 'size' is missing in type 'Buffer'. [2322] (parameter) data: Excel.Buffer
const blob = new Blob([data], { type: this.blobType });
I'm getting error at the above line, did I miss anything ??
This fixed my issue:
const blob = new Blob([data as BlobPart], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
Try to change the callback into this instead
workbook.xlsx.writeBuffer().then((data:ArrayBuffer) => {
const blob = new Blob([data], { type: this.blobType });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = this.excelFileName;
a.click();
})
UPDATE
If you can't cast it like that there is a pull-request waiting for you on this issue, what you can do if you don't want to wait is to manually change it in your index.d.ts imported for excel.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With