Closed anstosa closed 4 years ago
I am facing the exact same problem:
saveToExcel = (path: string, data: IItem[]) => {
let workbook = new Excel.Workbook();
workbook.creator = 'StorePro';
workbook.lastModifiedBy = 'Her';
workbook.created = Date.today();
workbook.modified = null;
workbook.lastPrinted = new Date(2016, 9, 27);
workbook.views = [{
x: 0, y: 0, width: 10000, height: 20000,
firstSheet: 0, activeTab: 1, visibility: 'visible'
}];
let ws1 = workbook.addWorksheet(
'Datasheet',
{ pageSetup: { paperSize: 9, orientation: 'landscape' } }
);
data.map(x => {
ws1.addRow(x).commit();
});
ws1.commit(); // PROBLEM HERE
const writeStream = fs.createWriteStream(path);
workbook.xlsx.writeBuffer({ stream: writeStream, useStyles: true, useSharedStrings: true })
writeStream.close();
}
I get the following error:
TypeError: ws1.commit is not a function
at ViewInventory._this.saveToExcel (webpack:///./src/components/Inventory/ViewInventory.tsx?:122)
at eval (webpack:///./src/components/Inventory/ViewInventory.tsx?:95)
Oh I figured this out and forgot to close. commit()
was not necessary for my workflow after all. I remove it from the rows and the worksheet and writing the buffer to the return stream worked just fine 🤷♂️
Hey @anstosa,
similar issue happen to me but with the error: TypeError: T.createWriteStream is not a function
.
I am using : "exceljs": "^3.4.0"
My code is:
import Excel from 'exceljs';
// OR
import * as Excel from 'exceljs';
export default () => {
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) });
// save under export.xlsx
workbook.xlsx.writeFile('export.xlsx');
Maybe you encountered in a similar error?
UPDATE
After additional research I understood that workbook.xlsx.writeFile
is not support currently from client side so I am using writeBuffer
and saving it with file-saver
package instead.
I'm sure I'm being an idiot and it's too late to be programming but I can't see it. plz help.