Skip to Content
avatar image
Former Member

sap.ui.table.Table export to excel

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; }

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • May 10, 2017 at 11:01 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

  • avatar image
    Former Member
    May 15, 2017 at 10:17 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • May 15, 2017 at 01:00 PM

    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">
    

    Add comment
    10|10000 characters needed characters exceeded