Not getting correct data when exporting JSON to Excel. (JavaScript)

Viewed 28

The goal is to be able to export JSON data into an xlsx-file. I have seccsesfully been able to create the file by pressing a button, but for some reason I do not get the correct data inside of the excel-file.

The data with console.log():

Console.log() data

What I get in the Excel-file:

Wrong excel

It seems like the excel-file has just written the amount of objects.

Small snippet of what i need it to look like:

Excel, what i need it to look like

JavaScript:


$( document ).ready(function() {
    $("#export").click(function() {
        const jsonData = excelExportData;
        var jsonDataObject = eval(jsonData);        
        exportWorksheet(jsonDataObject);
    }); 
});


function exportWorksheet(jsonObject) {
    var myFile = "myFile.xlsx";
    var myWorkSheet = XLSX.utils.json_to_sheet(jsonObject);
    var myWorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(myWorkBook, myWorkSheet, "myWorkSheet");
    XLSX.writeFile(myWorkBook, myFile);
  }

This is the code i have. excelExportData is the data that i am trying to convert.

Thanks in advance, and please ask if you want any extra information.

NOTE: The excelExportData is a variable that took data from an JSON-file and was filtered using for-loops and such. This process is happening in another js-file, not the same file as the code i shared.

0 Answers
Related