Search code examples
vue.jsnuxt.jsxlsxalasql

How to import data from Excel file in nuxt vue js app


I have this function in simple HTML page and that work fine

head () {
        return {
          script: [
            {
              hid: 'alasql',
              src: 'https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js',
              defer: true,
              //callback: () => { this.isStripeLoaded = true } 
            },  
            {
              hid: 'ajax',
              src: 'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.core.min.js',
              defer: true,
            
              
            }
          ]
        }
    },
  mounted(){

     function setPrice(nomer){
                    var printResult = function(result){ 
                            console.log(result[0].te)
                            // var sum= result[0].te
      
                        };
                        
        alasql ("SELECT te  FROM XLSX('/MDF_PANELS.XLS') WHERE f = "+nomer+"",[],
            function (data) {printResult(data);})                       
      }
}

But when I move to nuxt app I got error:

Invalid HTML: could not find <table>
    at e (xlsx.core.min.js:16)

My xls files hold in 'static' folder.

I will be glad for any help.


Solution

  • Firstly u need right import alasql

    import * as alasql from 'alasql';
    alasql["private"].externalXlsxLib = require('xlsx');
    

    Secondly use form from Read and write Excel and raw data files

    here my code:

    <template>
    
    </template>
    
    <script>
    import  alasql  from 'alasql';
    alasql["private"].externalXlsxLib = require('xlsx');
    
    export default {
        mounted(){      
     
     alasql.promise(
        "SELECT te  FROM XLSX('/MDF_PANELS.XLS') WHERE f = 13 ",[], ).then(function(results){
        console.log(results);
            }).catch(console.error);    
      }
        }
    </script>
    

    Where 'te' is the name of the required column and 'f' is the name of the required row