gpt4 book ai didi

excel - 将 XLS(x) 数据转换为 Angular Material 数据表

转载 作者:行者123 更新时间:2023-12-04 09:36:04 25 4
gpt4 key购买 nike

我正在做一个项目,我需要从 Excel 电子表格(最终是多张工作表)导入数据,并且我想在 Angular Material 数据表中显示数据。
我可以使用 npm - xlsx 模块将 Excel 数据作为 csv、json、html 或文本加载到我的组件中。
我的 HTML 页面工作正常,但任何传递数据的尝试都会给我一个空白表格。数据未与 mat-table 读取数据的方式正确对齐。我确实有一个标准的 HTML 表格,它可以正常工作并正确显示数据,但我确实需要来自 Angular Material 的功能。
这给了我这种格式的数据:

Block1: 
Data: 0: (5) ["SYMBOL", "QTY", "MFG", "PART NUMBER", "DESC"]
1: (5) ["TB2", 1, "AB", "1492-EBJ3", "PLASTIC END BARRIER, GRAY FOR AB 1492-J*"]
2: (5) ["TB2", 11, "AB", "1492-J4", "SINGLE CIRCUIT SCREW TERMINAL BLOCK, 22-10 AWG, GRAY, 35A, 4MM"]
3: (5) ["ECR1", 1, "AB", "700S-CF620DC", "CF TYPE SAFETY CONTROL RELAY, 120VAC COIL, 8 POLE, 6 NO 2 NC STANDARD CONTACTS"]
4: (5) ["SPARE", 5, "BUSSMANN", "AGC-1/2", "FAST ACTING, 1/2AMP, 250V, 1/4 X 1-1/4 INCH, GLASS TUBE FUSE"]
我需要将其转换为这种格式:
 Block2:
0: {partId: 1, partNumber: "*M-XB-12248", description: "Killark XB-12248 mod. w/holes", supplierId: 31, supplierName: "Kendall Electrical Supply", …}
1: {partId: 4, partNumber: "E9UVCRN", description: "1 Gang Vertical Box, Weatherproof", supplierId: 31, supplierName: "Kendall Electrical Supply", …}
2: {partId: 79, partNumber: "F66G36", description: "36" Straight Wireway Section", supplierId: 31, supplierName: "Kendall Electrical Supply", …}
3: {partId: 21, partNumber: "EMT1-1/4 ELB", description: "1 1/4" EMT Sweep", supplierId: 14, supplierName: "Graybar", …}
我正在查看两个 block 的格式,而不是 block 中的特定数据( block 2 来自正在读取 JSON 文件的应用程序的另一部分)。
这是我读取/解析 Excel 数据的 Angular 组件,我还想将数据转换为上面 Block2 中的类型。
import { Component, OnInit, Input, ViewChild, ElementRef } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import * as XLSX from 'xlsx';

export class BoMItem {
item: string;
symbol: string;
qty: number;
mfg: string;
partNumber: string;
desc: string;
}

@Component({
selector: 'app-bom-list',
templateUrl: './bom-list.component.html',
styleUrls: ['./bom-list.component.css']
})
export class BomListComponent implements OnInit {

data: [][];

@ViewChild('bomTable', { static: false }) bomTable: ElementRef;

// dataSource = new MatTableDataSource<BoMItem>();

dataSource = new MatTableDataSource<BoMItem>();
// displayedColumns: string[] = ['item', 'symbol', 'qty', 'mfg', 'partNumber', 'desc'];
displayedColumns: string[] = ['partNumber', 'mfg', 'desc', 'qty'];

importFile: File;
storeData: any;
csvData: any;
jsonData: any;
textData: any;
worksheet: any;
arrData: any;

constructor() {
}

ngOnInit() {
}

uploadedFile(evt: any) {
const target: DataTransfer = (evt.target) as DataTransfer;

const reader: FileReader = new FileReader();

reader.onload = (e: any) => {
let bstr: string = e.target.result;

let wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });

let wsName: string = wb.SheetNames[0];

let ws: XLSX.WorkSheet = wb.Sheets[wsName];

this.data = (XLSX.utils.sheet_to_json(ws, { header: 1 }));
console.log('Data: ', this.data);
this.jsonData = JSON.stringify(this.data);

console.log('JsonData: ', this.jsonData);

};

reader.readAsBinaryString(target.files[0]);
}

}
HTML:
  <table>
<tbody>
<tr *ngFor="let row of data">
<td *ngFor="let cell of row">
{{cell}}
</td>
</tr>
</tbody>
</table>

<div class="class=" mat-elevation-z8>
<table mat-table [dataSource]="dataSource" id="bomTable" #bomTable>

<ng-container matColumnDef="partNumber">
<th mat-header-cell *matHeaderCellDef> Part Number </th>
<td mat-cell *matCellDef="let element">{{ element.partNumber }}</td>
</ng-container>

<ng-container matColumnDef="mfg">
<th mat-header-cell *matHeaderCellDef> MFG </th>
<td mat-cell *matCellDef="let element">{{ element.mfg }}</td>
</ng-container>

<ng-container matColumnDef="desc">
<th mat-header-cell *matHeaderCellDef> Desc </th>
<td mat-cell *matCellDef="let element">{{ element.desc }}</td>
</ng-container>

<ng-container matColumnDef="qty">
<th mat-header-cell *matHeaderCellDef> QTY </th>
<td mat-cell *matCellDef="let element">{{ element.qty }}</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

</table>
</div>

最佳答案

发现我需要为 xlsx 模块中的 sheet_to_json 函数使用 raw: true 对象。

this.data = (XLSX.utils.sheet_to_json(ws, { raw: true }));
这为我提供了我正在寻找我的数据的格式。

关于excel - 将 XLS(x) 数据转换为 Angular Material 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62581090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com