exceljs 在 Angular

问题描述

当我创建一个 worksheet.columns = [] of data 时,它告诉我它是错误的。我正在使用 VSCode,下面有所有详细信息。 版本:1.52.1(用户设置) 提交:ea3859d4ba2f3e577a159bc91e3074c5d85c0523 日期:2020-12-16T16:34:46.910Z 电子:9.3.5 铬:83.0.4103.122 Node.js:12.14.1 V8:8.3.110.13-electron.0 操作系统:Windows_NT x64 10.0.17763

 worksheet.columns = [
  { key: 'QuestionTitle',width: 20 },{ key: 'RespondedBy',width: 25 },{ key: 'ResponseDateTime',{ key: 'Response',width: 50 },{ key: 'Tag',width: 20 }
];

在我看来以上是正确的。但是 VScode 告诉我这是错误的。

enter image description here

下面有完整的代码

public exportAsExcelFile(excelData: any[],excelFileName: string,surveyTitle: string,clientDateFormat: string): void {
const workbook = new ExcelJS.Workbook();

workbook.creator = 'My-Team';
workbook.lastModifiedBy = 'SSE';
workbook.created = new Date(2021,2,10);
workbook.modified = new Date();
workbook.lastPrinted = new Date();

// Set workbook dates to 1904 date system
workbook.properties.date1904 = true;

const worksheet = workbook.addWorksheet('Responses');

worksheet.mergeCells('A1','E2');
worksheet.getCell('C1').value = surveyTitle;

worksheet.getRow(3).values = ['Question Title','Responded By','Response Date & Time','Responses','Tags Name'];

worksheet.columns = [
  { key: 'QuestionTitle',width: 20 }
];


excelData.forEach(element => {
  console.log("element: ",element);

  if(element.ResponseDateTime != "")
  {
    element.ResponseDateTime = this.datepipe.transform(element.ResponseDateTime,clientDateFormat); 
  }

  const row = worksheet.addRow(element);
  //Make a boarder to data.
  row.eachCell(function (cell) {
    cell.border = {
      top: { style: 'thin' },left: { style: 'thin' },bottom: { style: 'thin' },right: { style: 'thin' }
    };
  })

 

});


workbook.xlsx.writeBuffer().then((buffer: any) => {
  console.log("buffer: ",buffer);
  const data: Blob = new Blob([buffer],{ type: EXCEL_TYPE });
  FileSaver.saveAs(data,excelFileName + EXCEL_EXTENSION);
});

}

任何帮助将不胜感激。

解决方法

在打字稿中使用时,这是exceljs库中的known bug, 你可以试试这个解决方法:


...
worksheet.columns = [
  { key: 'QuestionTitle',width: 20 },{ key: 'RespondedBy',width: 25 },{ key: 'ResponseDateTime',{ key: 'Response',width: 50 },{ key: 'Tag',width: 20 }
] as ExcelJS.Column[];