Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading local Excel file with js-xlsx using Angular 12?

I have my Angular-CLI frontend development server running locally on localhost:4200

I need to get a local Excel file stored on my PC, read its content and make some calls to an API from the client side.

I'm trying to use js-xlsx, got it installed with npm install xlsx but I can't find how to get the file and read its content.

How can I import a local excel file with js-xlsx in Angular 13?

like image 857
Iñigo Avatar asked Sep 06 '19 08:09

Iñigo


People also ask

How read data from Excel in AngularJS?

The Excel file (XLS and XLSX) will be selected in HTML FileUpload element and will be read using HTML5 FileReader API. The read data will be parsed into a JSON Array using the xlsx Excel plugin and later the JSON Array will be used to populate a HTML Table using ng-repeat directive in AngularJS.

How do I read a .XLSX file in typescript?

// read the excel file var workbook = XLSX. readFile('test. xlsx'); For reading a cell from the specific sheet, we have to provide an excel sheet name, or we can use the index of excel sheet to get the default assigned name for that excel sheet using the workbook package.


1 Answers

Here is working Example

onFileChange(ev) {
    let workBook = null;
    let jsonData = null;
    const reader = new FileReader();
    const file = ev.target.files[0];
    reader.onload = (event) => {
      const data = reader.result;
      workBook = XLSX.read(data, { type: 'binary' });
      jsonData = workBook.SheetNames.reduce((initial, name) => {
        const sheet = workBook.Sheets[name];
        initial[name] = XLSX.utils.sheet_to_json(sheet);
        return initial;
      }, {});
      const dataString = JSON.stringify(jsonData);
      document.getElementById('output').innerHTML = dataString.slice(0, 300).concat("...");
      this.setDownload(dataString);
    }
    reader.readAsBinaryString(file);
  }
like image 106
Chanaka Weerasinghe Avatar answered Oct 13 '22 07:10

Chanaka Weerasinghe