Justin
Justin

Reputation: 11

Edit-Reading xlsx file in Angular 5

I want to read xlsx file but what i want is just to read first three records because as you know if i read all the records, the browser will crash. I need your help to find a way to just read first three records (rows)

P.S: I don't want to save data in memory while parsing xlsx file

i am using this right now :

fileChange(event) {
    const fileList: FileList = event.target.files;
    if (fileList.length > 0) {
      const file: File = fileList[0];
      const reader = new FileReader();
      reader.onload = e => {
        const arrayBuffer = reader.result,
          data = new Uint8Array(arrayBuffer),
          arr = new Array();
        for (let i = 0; i !== data.length; ++i) {
          arr[i] = String.fromCharCode(data[i]);
        }

        const bstr = arr.join("");

        const workbook: XLSX.WorkBook = XLSX.read(bstr, { type: "binary" });
        const firstSheetName: string = workbook.SheetNames[0];
        const worksheet: XLSX.WorkSheet = workbook.Sheets[firstSheetName];
        this.setXlsxData(XLSX.utils.sheet_to_json(worksheet));
      };
      reader.readAsArrayBuffer(file);
    }
  }

  setXlsxData(data: Array<any>) {
    this.headers = Object.keys(data[0]);
    this.xlsxData = data;
  }

Upvotes: 0

Views: 1264

Answers (1)

Anmol Deora
Anmol Deora

Reputation: 41

try this library. I have written & read some xlsx files, it works smoothly. (Apologies, I couldnt write this in comment, can't comment)

https://www.npmjs.com/package/xlsx

Upvotes: 1

Related Questions