Search code examples
javajavascriptimport-from-excel

Best way to present data from a big excel file on a website


I have a big excel file and I want to show the data on my website. I have made a matlab and a Java program(script) that can extract the information I want from the excel file. How do I get the data to the website?

Should I have the matlab or java script running on a server and then make a javascript that in some way connects to the server? How would i do this?

Or should a make a javascript that directly reads the excel file?

I am a beginner in web development so I dont really know where to start...


Solution

  • I happened to do something like this while I had to analyze the grades I gave. What I did was export the Excel sheet as a CSV, and then put that in a website where I used JavaScript to first convert it to a JSON. This is the function I used to perform the conversion:

    function csvJSON(csv){
        var lines=csv.split("\n");
    
        var result = [];
    
        var headers=lines[0].split(",");
    
        for(var i=1;i<lines.length;i++){
    
            var obj = {};
            var currentline=lines[i].split(",");
    
            for(var j=0;j<headers.length;j++){
                obj[headers[j]] = currentline[j];
            }
            result.push(obj);
        }
    
        //return result; //JavaScript object
        return JSON.stringify(result); //JSON
    }
    

    And then it was simple. to use. Let's say I put in a textarea the exported CSV, for instance:

    Name,Grade1,Grade2
    Bobby,87,12
    Rakesh,9,3
    //...
    

    Then I called csvJSON() with this data to get:

    [{"Name":"Bobby","Grade1":87,"Grade2":12},{"Name":"Rakesh","Grade1":9,"Grade2":3},/*...*/]
    

    Which is especially easy to use. For instance:

    var csv = ""; // get it from somewhere
    var json = JSON.parse(csvJSON(csv));
    for (int i = 0; i < json.length; i++)
    {
        var obj = json[i];
        var name = obj.Name, Grade1 = obj.Grade1, Grade2 = obj.Grade2;
        //Do something
    }