Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format cells in workbook produced by xlsx package under Angular

NB. While primarily looking for an answer relating to the xlsx package (as it seems to be vastly suggested and discussed), I fear that it might be impossible. So, I'm also open to going in another direction (as long as it's free and works for Angular/Excel).

I've found xlxs and I'm producing Excel formatted files. I'd like to control a bit of the style so I added the property s to a cell as suggested in the comments (code sample here).

testMagic() {
  const table = document.getElementById("donkey");
  const workBook = XLSX.utils.table_to_book(table);
  workBook.Sheets.Sheet1.A1.s = { font: { bold: true } };
  XLSX.writeFile(workBook, "wonkey.xlsx");
}

I see no difference in the downloaded file, though. I've googled the issue but the combo Angular and xlsx and styling isn't commonly blogged.

I'm hoping it's something rather easy to resolve that I'm missing due to confusion and ignorance.

like image 838
Konrad Viltersten Avatar asked Mar 15 '19 10:03

Konrad Viltersten


2 Answers

I think you are using community(free) version of XLSX package. And the cell styling and other additional features to modify cell formats are only available in Pro version of the library. Please check here.

You can check official information here.

This is the community version. We also offer a pro version with performance enhancements, additional features like styling, and dedicated support.

P.S.- An alternative option to update the excel sheet and format the cells, use the exceljs npm package. you can see more details about styling here. See the example here(StackBlitz).

like image 129
Bhavik Patel Avatar answered Nov 07 '22 03:11

Bhavik Patel


This answer is a bit irrelevant to the question description but makes sense under this question title.

If you are wondering how to format a cell into the category 'text' since your data is automatically changing formats (number to date / phone number to exponential), then there is a parameter that can help while instantiating the Worksheet

raw:boolean

Use as:

const worksheet: XLSX.WorkSheet=XLSX.utils.table_to_sheet(tableElement, {raw:true});

like image 24
Ashique razak Avatar answered Nov 07 '22 02:11

Ashique razak