简体   繁体   English

上传/导入 Angular 中的 excel 表时,日期以数字格式显示

[英]Date is appearing in number format while upload / import excel sheet in Angular

I am trying to implement uploading the excel sheet using angular but when the sheet is displayed in browser date is changed to number format.我正在尝试使用 angular 实现上传 excel 工作表,但是当工作表显示在浏览器中时,日期更改为数字格式。 For ex.例如。 12-12-2020 is changed to 44177.00011574074. 2020 年 12 月 12 日更改为 44177.00011574074。 I need it in dd-mm-yyyy format.我需要 dd-mm-yyyy 格式。 Please guide me the changes I need to do in my typescript code.请指导我在 typescript 代码中需要做的更改。

app.component.html 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 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,
    })
}

Excel dates are stored as floating-point numbers. Excel 日期存储为浮点数。 The number 1.0 means 1900-01-01 00:00:00 .数字 1.0 表示1900-01-01 00:00:00 And other numbers mean days since that day.其他数字表示从那天起的天数。 So, you can convert an Excel date to a Javascript timestamp with something like this: const jsDate = (excelDate - 25569) * 24 * 60 * 60 * 1000 .因此,您可以将 Excel 日期转换为 Javascript 时间戳,如下所示: const jsDate = (excelDate - 25569) * 24 * 60 * 60 * 1000 The 25569 magic number is the number of days from 1900-01-01 to 1970-01-01 . 25569幻数是从1900-01-011970-01-01的天数。 24 * 60 * 60 * 1000 is the number of milliseconds in a day. 24 * 60 * 60 * 1000是一天中的毫秒数。

But the xlsx package's util.sheet_to_json() method does this for you if you give it the right options.但是,如果您给xlsx包的util.sheet_to_json()方法提供正确的选项,它会为您执行此操作。

this.worksheetData = XLSX.utils.sheet_to_json(this.worksheetInstance, {
      raw: false,
      header: this.worksheetHasHeader ? 0 : 1,
      dateNF: "dd/mm/yyyy"
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM