Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular 11 export excel with customize header

public exportAsExcelFile(json: any[], excelFileName: string, headersArray: any[]): void {
    //Excel Title, Header, Data
    const header = headersArray;
    const data = json;
    //Create workbook and worksheet
    let workbook = new Workbook();
    let worksheet = workbook.addWorksheet(excelFileName);
    //Add Header Row
    let headerRow = worksheet.addRow(header);
    // Cell Style : Fill and Border
    headerRow.eachCell((cell, number) => {
      cell.fill = {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFFFF00' },
        bgColor: { argb: 'FF0000FF' }
      }
      cell.border = { top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' } }
    })
    // Add Data and Conditional Formatting
    data.forEach((element) => {
      let eachRow = [];
      headersArray.forEach((headers) => {
        eachRow.push(element[headers])
      })
      if (element.isDeleted === "Y") {
        let deletedRow = worksheet.addRow(eachRow);
        deletedRow.eachCell((cell, number) => {
          cell.font = { name: 'Calibri', family: 4, size: 11, bold: false, strike: true };
        })
      } else {
        worksheet.addRow(eachRow);
      }
    })
    worksheet.getColumn(3).width = 15;
    worksheet.getColumn(4).width = 20;
    worksheet.getColumn(5).width = 30;
    worksheet.getColumn(6).width = 30;
    worksheet.getColumn(7).width = 10;
    worksheet.addRow([]);
    //Generate Excel File with given name
    workbook.xlsx.writeBuffer().then((data) => {
      let blob = new Blob([data], { type: EXCEL_TYPE });
      fs.saveAs(blob, excelFileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
    })
  }
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: jsconfig.json code to support absolute import 
Javascript :: javascript js ternary operater 
Javascript :: react-datepicker float position 
Javascript :: how to check if a browser is supported 
Javascript :: How to Check for an Empty String in JavaScript with the length Property 
Javascript :: hash url owl carousel example 
Javascript :: vue router push with params 
Javascript :: js variables 
Javascript :: nested ternary operator javascript 
Javascript :: react update version 
Javascript :: LocomotiveScroll npm 
Javascript :: node redirect 
Javascript :: xslt remove node 
Javascript :: js edit browser back location 
Javascript :: regular expression email 
Javascript :: ValueError: dictionary update sequence element #0 has length 1; 2 is required 
Javascript :: .change() in pure js 
Javascript :: repeat async call n times in js 
Javascript :: setTimeout() Method in javascript 
Javascript :: loading screen html css js 
Javascript :: defaultdeep lodash 
Javascript :: switch element array angular 
Javascript :: how to map array in react 
Javascript :: How to find out what character key is pressed?#key#keyCode#code 
Javascript :: dividing a number into digits javascript 
Javascript :: testing jest 
Javascript :: duplicate images in webpage js 
Javascript :: dart how to convert json to x-www-form-urlencoded 
Javascript :: how to find keycode in javascript 
Javascript :: remove text in div jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =