我想以Angular 将数据绑定到表控件,但表中没有填充数据.该表仅显示了我在HTML中定义的静态列名.首先,我从一个EXCEL文件中获取数据,并将其赋给一个名为ItemsArray的变量.这是我的代码

onFileChange(evt: any) {
    const target: DataTransfer = <DataTransfer>(evt.target);
  
    const reader: FileReader = new FileReader();
    reader.onload = (e: any) => {
    const bstr: string = e.target.result;
    const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });

    const wsname = wb.SheetNames[0];
    const ws: XLSX.WorkSheet = wb.Sheets[wsname];

    let data = (XLSX.utils.sheet_to_json(ws, { header: 1 })); 

    this.ItemsArray =data
    console.log(data);
    reader.readAsBinaryString(target.files[0]);
    
    }
  
  }

这是我的html代码

<div>

  <div style="margin: 50px auto;width: 50%;">
    <h2>Please select a file: </h2>
    <input type="file" (input)="onFileChange($event)" multiple="false">
  </div>

  <div>
    <h1>Angular Table with Excel Data</h1>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>ID</th>
          <th>Address</th>
          <th>Company</th>
          <th>Skills</th>
          <th>Email</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let item of ItemsArray">
          <th>{{ item.Name }}</th>
          <td>{{ item.ID }}</td>
          <td>{{ item.Address }}</td>
          <td>{{ item.Company }}</td>
          <td>{{ item.Skills }}</td>
          <td>{{ item.Email }}</td>
          <td>
            <button type="button">Edit</button>
            <button type="button">Delete</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>

</div>

当我调试代码时,我可以看到此行上的数据被赋值为ItemsArray

this.ItemsArray =data

如何解决此问题?我应该采取其他方法吗?

请找到下面的错误屏幕截图

enter image description here

第二个错误屏幕截图

enter image description here

我的EXCEL数据,但程序不应该依赖于EXCEL数据.

enter image description here

推荐答案

下面是一个有效的示例!

import { Component } from '@angular/core';
import { ExcelService } from './services/excel.service';
import * as XLSX from 'xlsx';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular';
  ItemsArray = [];

  onFileChange(evt: any) {
    const target: DataTransfer = <DataTransfer>evt.target;

    evt.target.files[0].arrayBuffer().then((buff: ArrayBuffer) => {
      let x = new Uint8Array(buff); // x is your uInt8Array
      // perform all required operations with x here.
      const wb: XLSX.WorkBook = XLSX.read(x, { type: 'array' });

      const wsname = wb.SheetNames[0];
      const ws: XLSX.WorkSheet = wb.Sheets[wsname];

      let data = XLSX.utils.sheet_to_json(ws, { header: 1 });
      const [cols, ...rows]: any = data;
      this.ItemsArray = rows.map((x: any) => {
        const row = {};
        x.forEach((x: any, i: number) => {
          row[<any>cols[i]] = x;
        });
        return row;
      });
      console.log(data);
    });
  }
}

stackblitz

Typescript相关问答推荐

忽略和K的定义

向NextAuth会话添加除名称、图像和ID之外的更多详细信息

无法从Chrome清除还原的初始状态数据

我可以为情态车使用棱角形的路由守卫吗?

如何使我的函数专门针对联合标记?

<;T扩展布尔值>;

ANGLING v17 CLI默认设置为独立:延迟加载是否仍需要@NgModule进行路由?

防止重复使用 Select 器重新渲染

有没有办法取消分发元组的联合?

自定义 Select 组件的类型问题:使用带逗号的泛型类型<;T与不带逗号的<;T&>;时出错

如何避免从引用<;字符串&>到引用<;字符串|未定义&>;的不安全赋值?

17个Angular 的延迟观察.如何在模板中转义@符号?

NPM使用Vite Reaction应用程序运行预览有效,但我无法将其部署到Netlify

回调函数中的TypeScrip类型保护返回Incorect类型保护(具有其他未定义类型的返回保护类型)

声明遵循映射类型的接口

如何在Reaction 18、Reaction-Rout6中的导航栏中获取路由参数

如何在Nextjs路由处理程序中指定响应正文的类型

如何在Angular /字体中访问API响应的子元素?

如何处理可能为空的变量...我知道这不是Null?

为什么`map()`返回类型不能维护与输入相同数量的值?