How to Extract/Read data from Excel document in Angular

 Extract/Read data from Excel document in Angular

EXCEL DATA


RESULT



Install node library:

npm i xlsx


Import XLSX Package

import * as XLSX from 'xlsx';

Install node library:

<input type="file" (change)="onFileChange($event)">

Event to extract data from Excel document in typescript:


onFileChange(event: any) {
    /* wire up file reader */
    const target: DataTransfer = <DataTransfer>(event.target);
    if (target.files.length !== 1) {
      throw new Error('Cannot use multiple files');
    }
    const reader: FileReader = new FileReader();
    reader.readAsBinaryString(target.files[0]);
    reader.onload = (e: any) => {
      /* create workbook */
      const binarystr: string = e.target.result;
      const wb: XLSX.WorkBook = XLSX.read(binarystr, { type: 'binary' });

      /* selected the first sheet */
      const wsname: string = wb.SheetNames[0];
      const ws: XLSX.WorkSheet = wb.Sheets[wsname];

      /* save data */
      const data = XLSX.utils.sheet_to_json(ws); // to get 2d array pass 2nd parameter as object {header: 1}
      console.log(data); // Data will be logged in array format containing objects
    };
 }


Demo:


Comments

Popular posts from this blog

Google Assistant Implementation in Android application with app actions