cancel
Showing results for 
Search instead for 
Did you mean: 

how to download json to excel sheet in sap ui5

Former Member
0 Kudos
2,060

Hi,

I searched the many forums but there is no use of me

I did the download .csv format but I need to download .xls format only

can you any once please help me on this...

I did for CSV:

 

  onClick: function() {

  var json = <jsondata>

  var csv = this.JSON2CSV(json);

  var fileName = "<filename>";

  var uri = 'data:text/csv;charset=utf-8,' + escape(csv);

  var link = document.createElement("a");

  link.href = uri;

  link.style = "visibility:hidden";

  link.download = fileName + ".csv";

  document.body.appendChild(link);

  link.click();

  document.body.removeChild(link);

  }

JSON2CSV: function(objArray) {

  var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

  var str = '';

  var line = '';

  var head = array[0];

  if ($("#quote").is(':checked')) {

  for (var index in array[0]) {

  var value = index + "";

  line += '"' + value.replace(/"/g, '""') + '",';

  }

  } else {

  for (var index in array[0]) {

  line += index + ',';

  }

  }

  line = line.slice(0, -1);

  str += line + '\r\n';

  for (var i = 0; i < array.length; i++) {

  var line = '';

  if ($("#quote").is(':checked')) {

  for (var index in array[i]) {

  var value = array[i][index] + "";

  line += '"' + value.replace(/"/g, '""') + '",';

  }

  } else {

  for (var index in array[i]) {

  line += array[i][index] + ',';

  }

  }

  line = line.slice(0, -1);

  str += line + '\r\n';

  }

  return str;

  },

View Entire Topic
former_member195440
Participant
0 Kudos

If you are getting your data from an OData service from Gateway, you could use $format=xlsx in the resource URI.

i.e. https://myhost.example.com/sap/opu/odata/sap/service/UserSet?$select=...&$filter=...&$format=$xlsx

This will get Gateway to output the data as an Excel file and is what the new Smart Template controls use.

Oli