问题
I am trying to read -> manipulate -> create excel. I am using streams
supported by exceljs
module. most of it is working fine, but the styles do not seem to be copied over. Below is my relevant part of the code.
const readerOptions = {
sharedStrings: 'cache',
hyperlinks: 'cache',
worksheets: 'emit',
styles: 'cache',
};
const writerOptions = {
filename: fpath,
useStyles: true,
useSharedStrings: true
}
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter(writerOptions)
const myworksheet = workbook.addWorksheet('Sheet 1');
const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader(sheet.path, readerOptions);
workbookReader.read();
workbookReader.on('worksheet', worksheet => {
worksheet.on('row', row => {
myworksheet.addRow(row.values).commit();
});
});
workbookReader.on('end', async () => {
await workbook.commit();
});
workbookReader.on('error', (err) => {
console.log(err);
});
please see the attached image for your reference. On left is the source excel file from which I am trying to manipulate and generate another excel and on right is the newly generated excel file from the source.
Any help on how to fix this will be really great, Thanks.
回答1:
For anyone stumbling on this post, below fix worked for me.
const readerOptions = {
sharedStrings: 'cache',
hyperlinks: 'cache',
worksheets: 'emit',
styles: 'cache',
};
const writerOptions = {
filename: fpath,
useStyles: true,
useSharedStrings: true
}
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter(writerOptions)
const myworksheet = workbook.addWorksheet('Sheet 1');
const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader(sheet.path, readerOptions);
workbookReader.read();
workbookReader.on('worksheet', worksheet => {
worksheet.on('row', row => {
const r = myworksheet.addRow();
Object.assign(r, row);
});
});
workbookReader.on('end', async () => {
await workbook.commit();
});
This copied the rows with all the correct styling information. (One issue, i am still facing is this does not work well with rows with merged cells)
来源:https://stackoverflow.com/questions/65309694/styles-not-getting-copied-from-source-excel-file