如何使用Angular TypeScript保护excel工作表?

9vw9lbht  于 2022-12-19  发布在  TypeScript
关注(0)|答案(1)|浏览(155)

我试过了,但还是做不好

**我尝试使用下面的代码,但不起作用

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);
  }

现在我想保护Excel工作表在运行时是可能的保护Excel文件.可以告诉我**

juzqafwq

juzqafwq1#

我使用SheetJS实现了这一点:Excel下载(){常量ws:XLSX.工作表= XLSX.实用程序.表格到工作表(此.TABLE1.本机元素);

// 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);

}

相关问题