Export Table data to csv file

Download the table data in csv format in excel



View.xml :

xmlns:l="sap.ui.layout"

<content>
<Table id="tableId" items="{/items}">


<columns>
<Column>
<Text text="First name" />
</Column>

<Column>
<Text text="last name" />
</Column>

<Column>
<Text text="gender" />
</Column>

<Column>
<Text text="hideone" />
</Column>

</columns>

<items>
<ColumnListItem>
<cells>
<Text text="{column1}" />
<Text text="{column2}" />
<Text text="{column3}" />
<Text text="{column4}" />

</cells>
</ColumnListItem>

</items>

</Table>
<Button icon="sap-icon://download" text="Download"
press="exportxls" id="download" />

</content>


controller.js :

jQuery.sap.require("sap/ui/core/util/Export");
jQuery.sap.require("sap/ui/core/util/ExportTypeCSV");

sap.ui.controller("exporttocsv.s1", {

onInit : function() {
var a = this.getView().byId("tableId");
var b = new sap.ui.model.json.JSONModel("model/data.json");
a.setModel(b);
sap.ui.getCore().setModel(b, "oModel");
},

exportxls : sap.m.Table.prototype.exportData || function(oEvent) {
var oModel = sap.ui.getCore().getModel("oModel");
var oExport = new sap.ui.core.util.Export({

exportType : new sap.ui.core.util.ExportTypeCSV({

separatorChar : ";"

}),

models : oModel,

rows : {
path : "/items"
},

columns : [ {
name : "First Name",
template : {
content : "{column1}"
}
}, {
name : "Last Name",
template : {
content : "{column2}"
}
}, {
name : "gender",
template : {
content : "{column3}"
}
}, {
name : "Address",
template : {
content : "{column4}"
}
},

]

});
oExport.saveFile().always(function() {
this.destroy();
});

}
});

make one folder inside webcontent name model. and make one file inside that name with data.jsonand paste following code inside that.


data.json :

{
"items": [
{
"column1": "tushar",
"column2": "patil",
"column3": "male",
"column4": "kharghar"
},
{
"column1": "digvijay",
"column2": "pawar",
"column3": "male",
"column4": "chalisgaon"
},
{
"column1": "richa",
"column2": "ghodake",
"column3": "femALE",
"column4": "ghatkopar"
}
]
}


output : 


chrome output : click download button


excel sheet in csv format

Post a Comment

Previous Post Next Post