How to convert all excel data to JSON in vuejs [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-12-15 05:17:10

问题


I am dynamically generating excel template file using vue-json-excel for the user. After user filling data in the template file i want take it as a input and convert all data in JSON formate and send it to the server side but not getting how to do .

<template>
  <div id="app">
    <md-field @change="previewFiles">
          <label>upload excel file</label>
          <md-file v-model="metaDataFile" @change="previewFiles" />
    </md-field>
  </div>
</template>

<script>
    export default {
      components: {
        loginComponent,
        uploadComponent
      },
      data() {
        return {
          metaDataFile: null
        }
      },
      methods:{
        previewFiles(event) {
          console.log(event.target.files);
          console.log(this.metaDataFile);
       }
      }
    }

</script>

回答1:


this worked for me

 previewFiles(e) {
          var files = e.target.files, f = files[0];
          var reader = new FileReader();
          reader.onload = function(e) {
            var data = new Uint8Array(e.target.result);
            var workbook = XLSX.read(data, {type: 'array'});
            let sheetName = workbook.SheetNames[0]
            /* DO SOMETHING WITH workbook HERE */
            console.log(workbook);
            let worksheet = workbook.Sheets[sheetName];
            console.log(XLSX.utils.sheet_to_json(worksheet));
          };
          reader.readAsArrayBuffer(f);
       }



回答2:


Excel files is basically a zipfile with raw data in ... You can do this manualy or using a lib for that ...

You can try this one : https://github.com/sheetjs/js-xlsx



来源:https://stackoverflow.com/questions/57672887/how-to-convert-all-excel-data-to-json-in-vuejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!