Skip to Content
avatar image
Former Member

How to remove space and comma in csv download files

Hi All,

I am new to sapui5. I have downloading ui5 tables as CSV format by refereed below link.

Export sap.ui.table.Table as CSV

Those tables data i am getting from XSODATA Service. service returning result as Json Format.

Now the problem is my CSV file having %20 and %2 instead of space and comma values(if I remove enCodeURIComponent, values are misplacing in excel cells which is values having comma and space). I have added my code snippets below.

View.js

tableObj = sap.ui.getCore().byId("myTableId");
var oModel = new sap.ui.model.json.JSONModel();
jQuery.ajax({
                   url: url,  
                 dataType: "json",
               beforeSend: function(){
                tableObj.setBusy(true);
           },
            success: function(data, textStatus, jqXHR) { 
                totalResultsCount = data.d.results.length;
               var b64text = generateTableCSV(tableObj,data.d.results);
                    sap.ui.getCore().byId("id_exportcsvbutton").setHref('data:application/csv;charset=utf-8;base64,' + (Base64.encode(b64text)));
                    setTimeout(function (){
                        $( "#Myprgs_exportcsv" ).attr('download', 'MyProgress.csv');
                    },1000); 
          error: function(jqXHR, textStatus, errorThrown) {

            },
            complete: function(){
         tableObj.setBusy(false);
           }
        });

downloadExcel.js

function generateTableCSV(table, jsonData) {
    for (var i = 0; i < table.getColumns().length; i++) {        
    info += encodeURIComponent(table.getColumns()[i].getLabel().getText())
            + ',';                
        }            
        }
    info += '\r\n';
    
    if (jsonData.length != undefined) {
        for (var j = 0; j < jsonData.length; j++) {            
            for (var i = 0; i < table.getColumns().length; i++) {                
                var dk=  table.getColumns()[i].getTemplate().getBinding(
                'value').sPath;
                if (table.getColumns()[i].getTemplate() != undefined 
                && table.getColumns()[i].getTemplate().getBinding(
                                'value') != undefined) {
                    var valor = eval('jsonData[j].'+ dk);    
                    info += encodeURIComponent(valor) + ',';    
                    } 
            }
            info += '\r\n';
            }
    } else {
        $.each(jsonData, function(key, value) {
            for (var i = 0; i < table.getColumns().length; i++) {                
                var dk=  table.getColumns()[i].getTemplate().getBinding('value').sPath;
                if (table.getColumns()[i].getTemplate() != undefined && table.getColumns()[i].getTemplate().getBinding('text') != undefined) {
                    var valor = eval('jsonData[j].'+ dk);
                    info += encodeURIComponent(valor) + ',';
                }                 
            }
            info += '\r\n';
        });
    }
    return info;
}

Base 64 Util.js

I have using same code which is in this link Export sap.ui.table.Table as CSV

Base 64 Util.

also I have attached my CSV output as screenshot. Can some one help me to resolve this?? Any help would be greatly appreciated.

Thanks,

Jayan.M

csvoutput.png (69.2 kB)
Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    avatar image
    Former Member
    Apr 10, 2016 at 04:29 AM

    Hi Jayan,

    The problem is your file cells contains "," which at the same time is your csv separator.

    info += encodeURIComponent(valor) + ',';


    You should change your csv separator from"," to ";" or another one.

    This way you won't have this issues anymore.

    Once you change your separator, the previous sentence would look like this:

    info += valor + ';';


    In thenext post you can find the explanation on how to change your csv file separator.

    Import or export text (.txt or .csv) files - Excel

    Regards,

    Emanel

    Add comment
    10|10000 characters needed characters exceeded