Home > Net >  How to protect the excel worksheet using Angular TypeScript?
How to protect the excel worksheet using Angular TypeScript?

Time:12-17

I tried but could not get it right

**I tried using the below code but it didn't work

import * as XLSX from 'xlsx';
edxporttoexcel() {
    const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(this.TABLE1.nativeElement);
    ws.protect()
    ws.protect('abc123')
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'datareq');
    let filename="filenatest.xlsx"
    XLSX.writeFile(wb, filename);
  }

now I want to protect the excel sheet at run time is it possible to protect excel file. can anyone tell me **

CodePudding user response:

I achieved this using SheetJS: exceldownload() { const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(this.TABLE1.nativeElement);

// custom format 
if (this.BANKNAME == 'HDFC BANK LTD') {
  for (var i in ws) {
    console.log(ws[i]);
    if (typeof ws[i] != 'object') continue;
    let cell = XLSX.utils.decode_cell(i);
    ws['!cols'] = [];
    ws['!cols'] = [
      { 'width': 10 }, // width for col A
      { 'width': 18 },
      { 'width': 10 },
      { 'width': 10 },
      { 'width': 10 },
      { 'width': 15 },
      { 'width': 10 },
      { 'width': 10 },
      { 'width': 15 },
      { 'width': 10 }, { 'width': 10 }, { 'width': 18 }, { 'width': 10 }, { 'width': 10 }, { 'width': 18 }, { 'width': 10 }, { 'width': 18 }, { 'width': 15 }, { 'width': 15 }, { 'width': 10 }, { 'width': 10 }, { 'width': 15 }, { 'width': 10 }, { 'width': 10 }, { 'width': 10 }, { 'width': 10 }, { 'width': 10 }// width for col B
    ]; 
    //protect
    ws['!protect'] = {};

    ws['!protect'] =
    {
      password: 'C@1235#' 

    }


    ws[i].s = {

      // styling for all cells
      font: {
        name: 'arial',
      },
      alignment: {
        vertical: 'center',
        horizontal: 'center',
        wrapText: '1', // any truthy value here
      },
      border: {
        right: {
          style: 'thin',
          color: '000000',
        },
        left: {
          style: 'thin',
          color: '000000',
        },
      },
    };
    if (cell.r == 0) {
      // first row
      ws[i].s.border.bottom = {
        // bottom border
        style: 'thin',
        color: '#c30c0c',
      };
    }

    if (cell.r % 3) {
      if (cell.c == 1 || cell.c == 11 || cell.c == 14 || cell.c == 16) {
        ws[i].s.numFmt = '0.00';
        ws[i].z = '0';
        //ws[i].s.width=50
        //ws[i].s.alignment = { wrapText: true }

      }
      else
        if (cell.c == 5 || cell.c == 8 || cell.c == 21) {
          // first column
          ws[i].s.numFmt = 'DD-MMM-YYYY'; // for dates
          ws[i].z = 'DD-MMM-YYYY';
        }
        else {
          ws[i].s.Number = '00'; // other numbers
        }
    }

    if (cell.r % 3) {

      // every other row
      ws[i].s.fill = {
        // background color
        patternType: 'solid',
        fgColor: { rgb: 'b2b2b2' },
        bgColor: { rgb: 'b2b2b2' },
      };
    }
  }
}

const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'LabelData');
let fname = this.ACCOUNT   "_"   this.DIVYEAR   ".xlsx"
XLSX.writeFile(wb, fname);

}

  • Related