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



我试过了,但是没有成功

**我尝试使用下面的代码,但它没有工作

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文件。谁能告诉我* *

我使用SheetJS实现了这一点:exceldownload () {const ws: XLSX。

工作表= XLSX.utils.table_to_sheet(this. table . 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);

}

最新更新