-2

I am able to export table data to excel dynamically, but some of the data like long numbers and negative values are formatted

Below is the UI Table data [enter image description here][1]

exported data to CSV [enter image description here][2]

when I select the long values it showing the proper value on top of excel, and -ve values are appended with '......

Code :

jQuery.sap.require("sap.ui.core.util.Export");
jQuery.sap.require("sap.ui.core.util.ExportTypeCSV");
sap.ui.define([
'jquery.sap.global',
'sap/ui/core/mvc/Controller',
'sap/ui/model/json/JSONModel'
], function(jQuery, Controller, JSONModel) {
"use strict";
var vSrcMATNR;
var oEvtBus = sap.ui.getCore().getEventBus();
var TableController = Controller.extend("UtilLoad.Util", {
exportToExcel: function(oTable) {
var that = this;
var aColumns = oTable.getColumns();
var aItems = oTable.getItems();
var aTemplate = [];
for (var i = 0; i < aColumns.length; i++) {
var oColumn = {
name: aColumns[i].getHeader().getText(),
template: {
content: {
path: null
}
}
};
if (aItems.length > 0) {
oColumn.template.content.path = aItems[0].getCells()[i].getBinding("text").getPath();
}
aTemplate.push(oColumn);
}
var oExport = new sap.ui.core.util.Export({
// Type that will be used to generate the content. Own ExportType’s can be created to support other formats
exportType: new sap.ui.core.util.ExportTypeCSV({
separatorChar : ",",
                charset : "utf-8"
}),
// Pass in the model created above
models: oTable.getModel(),
// binding information for the rows aggregation
rows: {
path: "/"
},
// column definitions with column name and binding info for the content
columns: aTemplate
});
oExport.saveFile().always(function() {
this.destroy();
});
},
fnDate: function(sDate) {
if (sDate) {
var oDateFormat = sap.ui.core.format.DateFormat.getDateTimeInstance({
pattern: "dd-MM-yyyy"
});
return oDateFormat.format(new Date(sDate));
} else {
return sDate;
}
}
});
return TableController;
});

from SAP documentation advicing to use data export functionality , please what is it and how

https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.core.util.ExportTypeCSV.html

Thanks Rajesh

3
  • Could you please explain your issue in more detail so that we can be sure to understand your question?
    – user6177399
    Apr 4, 2017 at 7:26
  • i.stack.imgur.com/AnoWl.jpg Apr 5, 2017 at 8:38
  • in generated excel negative values are prepended with single quote Apr 5, 2017 at 8:41

4 Answers 4

2

Please convert that numbers into string by giving Tab Space between double quotes in the return statement.

            {
                name: "Stock",
                template: {
                    content: {
                        parts: ["StockPosition"],
                        formatter: function(value) {

                            return "    " + value;
                        }
                    }
                }
            }
3
  • Thanks it works, can you help how can avoid showing longvalues in formaul like 2.85E+12, please find below snap i.stack.imgur.com/AnoWl.jpg Apr 5, 2017 at 11:09
  • Yes, in my logic for except the DATE values all other fields I am applying this formatter Apr 6, 2017 at 8:55
  • Out of curiosity, how to implement below excel steps ->rightclick(on column)->format cells->fraction(from catageories) Apr 6, 2017 at 15:02
0

please find below screen shot exported excel data

0

vH,

Yes,main concern is while transforming data to excel.By appending tab character (\x09) I can avoid scientific notation in excel.

Not sure is the best solution or not

Thanks Rajesh

-1

Concerning your problem with values being formatted in scientific notation like 2.85E+12:

it is generally not a good idea to deal with precise large numbers in JavaScript, because JavaScript uses double precision floating point numbers as defined in the IEEE 754 standard. They lose precision very quickly!

Just try

console.log(9999999999999999);

the result will be

10000000000000000

If you want to avoid scientific notation in JavaScript, you have to write your own toString-function by concatenating each digit

function convertToString(fNumber){
 var sResult='';
 do{
   var iDigit = fNumber%10;
   fNumber=Math.trunc(fNumber/10);
   sResult = iDigit+sResult;
 }while(fNumber>0)   
 return sResult;
}

But your main problem is Excel!

If your csv-file looks like this

Customer;Product;EAN;Price
SAP;Hana;1234567890123;1000
SAP;R/3;1234567890124;-1000
SAP;OpenUI5;1234567890126;9999999999999999

Excel will show a table that contains scientific notation again!

Customer   Product   EAN       Price
SAP        Hana     1,23E+12     1000
SAP        R/3      1,23E+12    -1000
SAP        OpenUI5  1,23E+12   1E+16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.