styles not getting copied from source excel file

这一生的挚爱 提交于 2021-01-07 01:24:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!