Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using the below code where I am using , I am looking for a way where I can fix the filename with the proper path.

import { Component, OnInit } from '@angular/core';
import * as XLSX from 'xlsx';

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

  data: [][];
  constructor() {}

  ngOnInit(): void {
  }

  onFileChange(evt: any) {
    const target: DataTransfer = < DataTransfer > (evt.target);

    //const target : File ='Demo.XLSX'; 

    if (target.files.length !== 1) throw new Error('Cammot use multiple files');

    const reader: FileReader = new FileReader();

    reader.onload = (e: any) => {
      const bstr: string = e.target.result;
      const wb: XLSX.WorkBook = XLSX.read(bstr, {
        type: 'binary'
      })
      const wsname: string = wb.SheetNames[0];
      const ws: XLSX.WorkSheet = wb.Sheets[wsname];
      console.log(ws);
      this.data = (XLSX.utils.sheet_to_json(ws, {
        header: 1
      }));
      console.log(this.data);
    }
    reader.readAsBinaryString(target.files[0]);
  }
}

I am using a drag a drop feature whereas I want to open a fix excel file, any idea how may I do this?

question from:https://stackoverflow.com/questions/65648578/how-to-open-a-fix-excel-file-in-angular-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
76 views
Welcome To Ask or Share your Answers For Others

1 Answer

1   

addfile(event)?????
    2   ??{????
    3   ??this.file=?event.target.files[0];?????
    4   ??let?fileReader?=?new?FileReader();????
    5   ??fileReader.readAsArrayBuffer(this.file);?????
    6   ??fileReader.onload?=?(e)?=>?{????
    7   ??????this.arrayBuffer?=?fileReader.result;????
    8   ??????var?data?=?new?Uint8Array(this.arrayBuffer);????
    9   ??????var?arr?=?new?Array();????
    10  ??????for(var?i?=?0;?i?!=?data.length;?++i)?arr[i]?=?String.fromCharCode(data[i]);????
    11  ??????var?bstr?=?arr.join("");????
    12  ??????var?workbook?=?XLSX.read(bstr,?{type:"binary"});????
    13  ??????var?first_sheet_name?=?workbook.SheetNames[0];????
    14  ??????var?worksheet?=?workbook.Sheets[first_sheet_name];????
    15  ??????console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));????
    16  ????????var?arraylist?=?XLSX.utils.sheet_to_json(worksheet,{raw:true});?????
    17  ????????????this.filelist?=?[];????
    18  ????????????console.log(this.filelist)????
    19  ????
    20  ??}????
    21  }????

<input?type="file"?class="form-control"?(change)="addfile($event)"?placeholder="Upload?file"?accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,?application/vnd.ms-excel">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...