我正在尝试使用 angular 实现上传 excel 工作表,但是当工作表显示在浏览器中时,日期更改为数字格式。例如。2020 年 12 月 12 日更改为 44177.00011574074。我需要 dd-mm-yyyy 格式。请指导我在我的打字稿代码中需要做的更改。
app.component.html
<button button mat-raised-button class="btn-primary" color="primary" style="margin: 1%;"
(click)="triggerFileSelector($event)" style="margin: 1%;">
Choose File
</button>
<!-- Code to display table -->
<section class="section">
<table class="material-table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tr *ngFor="let rowData of tableData">
<td value="Delete" (click)="deleteRow(rowData)">X</td>
<td *ngFor="let schema of tableSchema"
[ngStyle]="{'background-color': (rowData[schema.field].value) ? 'white' : '#ff9999' }">
<span #el contenteditable (blur)="rowData[schema.field].value = el.innerText">
{{ rowData[schema.field].value }}
</span>
</td>
</tr>
</table>
</section>
app.component.ts
triggerFileSelector(e: any): void {
e.preventDefault()
this.fileInput.nativeElement.click()
}
handleFileInput(files: FileList): void {
this.workbookFile = files.item(0)
this.workbookFileName = this.workbookFile.name
this.readFile()
}
readFile(): void {
const temporaryFileReader = new FileReader()
temporaryFileReader.onerror = () => {
temporaryFileReader.abort()
return new DOMException('Problem parsing input file.')
}
temporaryFileReader.onload = (e: any) => {
/* read workbook */
const bstr: string = e.target.result
this.workbookInstance = XLSX.read(bstr, { type: 'binary' })
/* grab first sheet */
this.worksheetName = this.workbookInstance.SheetNames[0]
this.readSheet(this.worksheetName)
}
temporaryFileReader.readAsBinaryString(this.workbookFile)
}
readSheet(sheetName: string) {
this.worksheetInstance = this.workbookInstance.Sheets[sheetName]
this.worksheetData = XLSX.utils.sheet_to_json(this.worksheetInstance, {
header: this.worksheetHasHeader ? 0 : 1,
})
}