Skip to Content
0

sap.ui.table.Table export to excel

May 09, 2017 at 05:30 PM

139

avatar image

Exporting to excel works for SAP.M.TABLE (see code below that works for sap.m.table) but having issues when trying SAP.UI.TABLE.TABLE here's the code that works for sap.m.table but not for sap.ui.table.table.

This part I've tried different ways for the binding of the sap.ui.table.table and no luck any suggestions for this part

" (if (aItems.length > 0) {

oColumn.template.content.path = aItems[0].getCells()[i].getBinding("text").getPath();
} "

This works below for sap.m.table

==============================================================

var aColumns = getColumns(tbl);

var oExport = new sap.ui.core.util.Export({
exportType : new sap.ui.core.util.ExportTypeCSV({
separatorChar : ",",
charset : "utf-8"
}),
models : oModel1,
rows : {
path : tbl.getBinding("items").getPath(),
},
columns : aColumns
});
oExport.saveFile().always(function() {
this.destroy();
});
}

function getColumns(tbl) {

var aColumns = tbl.getColumns();
var aItems = tbl.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);
}
return aTemplate; }

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

VIPLOVE KHUSHALANI May 10, 2017 at 11:01 AM
0

Hi Jesse Brook

In getColumns function for sap.ui.table.table will be totally different , you need to see the aggregations of the sap.ui.table.table.

like :

aColumns[i].getHeader().getText(), // will give error in ui.table

aColumns[i].getLabel() // will be correct way to get header text ..

similarly you need to check for row items too

referece :

https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.table.Table.html

thanks

Viplove

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hi Viplove, I agree for the header text and that is not where I'm having a problem it is with the row items. Get the correct syntax for the row items. " oColumn.template.content.path = aItems[0].getCells()[i].getBinding("text").getPath(); "

I've tried different ways and I either get an undefined error or just the object Object in the cells.

thanks

0
Jesse Brook May 15, 2017 at 10:17 AM
0

Hi Viplove, I agree I'm ok for the header text, that is not where I'm having a problem it is with the row items. Getting the correct syntax for the row items. " oColumn.template.content.path = aItems[0].getCells()[i].getBinding("text").getPath(); "

I've tried different ways and I either get an undefined error or just the object Object in the cells.

thanks

Share
10 |10000 characters needed characters left characters exceeded
Abdul Moh'd May 15, 2017 at 01:00 PM
0

Hi,

Here's the export to XML function I wrote for our apps to use with sap.ui.table.table, seems a little complicated as it's written for re-usability.

Controller:

onDataExport: function(oEvent) {
                var tableId = oEvent.getSource().getAriaDescribedBy().toString();
                var oTable = sap.ui.getCore().byId(tableId);
                var oExport = oTable.exportData();
                var sModel = oTable.data("mainmodel");
                if (sModel){
                var aExpCol = oExport.getColumns();
                    var aCol = oTable.getColumns();
                    aCol.forEach(function(oColumn,i){
                    var oCell = new sap.ui.core.util.ExportCell();
                    console.log(oCell.getMetadata());
                    if (oColumn.data("ctype") === "DatePicker"){
                            oCell.bindProperty("content",{ path: sModel+">"+oColumn.getSortProperty(), formatter: formatter.getDateFormat  });
                    aExpCol[i].setTemplate(oCell);
                    } else if (oColumn.data("ctype") === "TimePicker"){
                    oCell.bindProperty("content",{ path: sModel+">"+oColumn.getSortProperty(), formatter: formatter.getTimeFormat  });
                    aExpCol[i].setTemplate(oCell);
                    }
                    });
                } 
                oExport.saveFile("Table_data" + new Date());
            },

app:mainmodel contains the name of the model bound to the table, the same as getBinding("rows"). I was experimenting with the data() method to attach data objects to controls and ended up using this over getBinding("rows") but both should work.

View:

<table:Table id="d_terrainTableID" rows="{terrains>/}"
		visibleRowCount="5" selectionMode="Single" enableColumnFreeze="true"
		rowSelectionChange="onTableRowSelect" app:model="terraindetails" app:mainmodel="terrains">

Share
10 |10000 characters needed characters left characters exceeded