Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 将excel数据转换为Json字符串时出错。FileReader.onload不工作_Angular_Angular7_Npm Install_Filereader_Openxlsx - Fatal编程技术网

Angular 将excel数据转换为Json字符串时出错。FileReader.onload不工作

Angular 将excel数据转换为Json字符串时出错。FileReader.onload不工作,angular,angular7,npm-install,filereader,openxlsx,Angular,Angular7,Npm Install,Filereader,Openxlsx,我使用XLSX npm包从Excel读取数据并转换为JSON格式 我用的是角度7 const reader=新文件读取器; const file=ev.target.files[0]; reader.onload=事件=>{ const data=reader.result; 控制台日志数据; 工作簿=XLSX.readdata,{type:'binary'}; jsonData=workBook.SheetNames.reduceinitial,name=>{ const sheet=work

我使用XLSX npm包从Excel读取数据并转换为JSON格式

我用的是角度7

const reader=新文件读取器; const file=ev.target.files[0]; reader.onload=事件=>{ const data=reader.result; 控制台日志数据; 工作簿=XLSX.readdata,{type:'binary'}; jsonData=workBook.SheetNames.reduceinitial,name=>{ const sheet=workBook.Sheets[名称]; 首字母[名称]=XLSX.utils.sheet_至_jsonsheet; 返回首字母; }, {}; const dataString=JSON.stringifyjsonData; }; 尝试使用reader.onload和reader.onloadend,它甚至没有抛出错误,但事件没有触发。 有人能帮我怎么做吗?
提前感谢。

事件不会触发,因为您没有对文件对象调用任何读取方法。要触发其中一个事件,需要对文件对象调用一个读取方法

文件读取器的读取方法是-

事件处理程序是-

您可以通过单击其中一个方法来阅读有关reader方法用法的更多信息,例如-

基本上,在您的代码中,您只需要在末尾添加一些read方法,如-

     const reader = new FileReader();
     const file = ev.target.files[0];
     reader.onload = (event) => {
       const data = reader.result;
       console.log(data);
       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);
     };

    // ADD THIS IN YOUR CODE
    reader.readAsDataURL(file);
这里有工作

 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);
  }