Reputation: 256
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 ??
Upvotes: 12
Views: 46428
Reputation: 311
This fixed my issue:
const blob = new Blob([data as BlobPart], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
Upvotes: 31
Reputation: 1
Try this:
workbook.xlsx.writeBuffer().then((data: Blob) => {
let blob = new Blob([data], { type: this.blobType });
FileSaver.saveAs(blob, 'name_of_the_file.xlsx');
});
Upvotes: -1
Reputation: 1547
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
Upvotes: 4