1

I am using xlsx to import excel and display as a table. The functionality is working fine. But there seem to be a small problem. If the cell value contains ',' then it creates a problem since xlsx javascript uses ',' as the delimiter.

Please find below the code snippet!

function filePicked(oEvent) {
// Get The File From The Input
var oFile = oEvent.target.files[0];
var sFilename = oFile.name;
// Create A File Reader HTML5
var reader = new FileReader();
// Ready The Event For When A File Gets Selected
reader.onload = function(e) {
    var data = e.target.result;
    var workbook = XLSX.read(data, {type : 'binary'});
    workbook.SheetNames.forEach(function(sheetName){
        // Here is your object
        var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
        var data = XLSX.utils.make_json(workbook.Sheets[sheetName], {header:1}); 
        var json_object = JSON.stringify(XL_row_object);
        alert(json_object);
        $.each(data, function( indexR, valueR ) {
            var sRow = "<tr>";
            $.each(data[indexR], function( indexC, valueC ) {
                sRow = sRow + "<td>" + valueC + "</td>";
            });
            sRow = sRow + "</tr>";
            $("#my_file_output").append(sRow);
        });
        //alert(json_object);

    })
};

// Tell JS To Start Reading The File.. You could delay this if desired
reader.readAsBinaryString(oFile);

}

Can anyone please guide how to overcome this.

Thanks in advance

rafavinu
  • 315
  • 4
  • 20

1 Answers1

-1

Just wrap it in double quotes

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

it's similar to dealing with commas in csv

Community
  • 1
  • 1
Joshua Duxbury
  • 4,430
  • 4
  • 29
  • 49