0

I have a code to read data from excel using Angular Typescript. Its working file till converted to JSON. But didn't fetch into HTML Page. Problem is in column name of Excelsheet.

Excel file column names are like (i) Autodesk Material Description (ii) Autodesk SAP Material Description

If I use in HTML {{pricedata.Autodesk Material Description}}, {{pricedata.Autodesk SAP Material Description}}. It gives me error.

So I converted that name into (i) Autodesk_Material_Description (ii) Autodesk_SAP_Material_Description IN EXCELSHEET and code too ({{pricedata.Autodesk_Material_Description}}, {{pricedata.Autodesk_SAP_Material_Description}}).

Now it's working fine. So, how to handle this problem ?

import { Component, HostListener } from '@angular/core';
import * as XLSX from 'xlsx';
  
@Component({
    selector: 'app-importpricelist',
    templateUrl: './importpricelist.component.html',
    styleUrls: ['./importpricelist.component.css']
  })
export class ImportpricelistComponent
{
  ExcelData:any;
  constructor(){

  }

  ReadExcel(event:any){

    let file = event.target.files[0];
    let fileReader = new FileReader();
    fileReader.readAsBinaryString(file);

    fileReader.onload = (e) => {
      var workBook = XLSX.read(fileReader.result, {type:'binary'});
      var SheetNames = workBook.SheetNames;
      this.ExcelData = XLSX.utils.sheet_to_json(workBook.Sheets[SheetNames[0]]);
      console.log(this.ExcelData);
  
    }
  }
}
  • `{{pricedata['Autodesk Material Description']}}` see: [How can I access a JavaScript object which has spaces in the object's key?](https://stackoverflow.com/questions/8317982/how-can-i-access-a-javascript-object-which-has-spaces-in-the-objects-key) – traynor Feb 08 '23 at 09:52
  • @traynor working perfectly thanks a lot. My mistake is in my syntax – Rohit Tarang Feb 09 '23 at 12:06

0 Answers0