Iggsy
Iggsy

Reputation: 113

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?

Upvotes: 3

Views: 8320

Answers (1)

Arik neKrol
Arik neKrol

Reputation: 68

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 */

Upvotes: 1

Related Questions