Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize my XLSX using TypeScript/Angular?

I have json:

{
   "cost": 852.14,
   "gross":741.85,
   "net": 213.00,
   "quantity":30,
   "missing": 20,
   "waiting":5
}

And this is my code:

  const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.reportPayment,
    {
      header: ['cost', 'gross', 'net', 'quantity','missing', 'waiting']
    });
    const workbook: XLSX.WorkBook = { Sheets: { 'facture': worksheet }, SheetNames: ['facture'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
    this.saveAsExcelFile(excelBuffer, 'Faktura');


  private saveAsExcelFile(buffer: any, fileName: string): void {
    const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });
    FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
  }

And I want to customize my XLSX, so these are my questions:

  1. How to merge any cell (in row direction and column)? (For example merge cell A1 with A2)
  2. How to set width and height cell? ()
  3. How to paste every single value of json to a chosen cell?
  4. How to set a background color to the cell?
like image 466
Iggsy Avatar asked Dec 31 '25 22:12

Iggsy


1 Answers

It is not about typescript or angular. It is all about SheetJs library (XLSX). So you need to read SheetJs documentation.

For example to merge cells you need to fill worksheet property "!merges", before you create XLSX.WorkBook:

if(!worksheet['!merges']) 
   worksheet['!merges'] = [];
worksheet["!merges"].push({s:{r:0,c:0},e:{r:1,c:0}}); /* A1:A2 */
like image 77
Arik neKrol Avatar answered Jan 03 '26 12:01

Arik neKrol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!