Joey De Laat
Joey De Laat

Reputation: 136

Exporting model to csv

I'm trying to make an export of my model which consists out of employees, some properties and a Date.

Visualisation of my model

var myModel= {
  employees: [{
    pentagram: oMyData.Pentagram,
    records: [{
      Date: oMyData.Date,
      GC: oMyData.Lunch,
      CheckIn: oMyData.CheckedIn,
      CheckOut: oMyData.CheckedOut
    }]
  }]
}; 

Purpose of my application

My app is used to log which employee is in the building by letting them check in and out at the front door. This is registered to a HanaXS database. Each day when an employee checks in, a record is created with the corresponding properties. So if the decide not to eat at the office, they will click on the GC button (which stands for 'no consumption' in dutch).

So in a nutshell. Each employee has their own record per date in the database.

What do I want to do

I want to make an excel sheet which will cover a month. The most left column will cover the names of the employee's (Pentagram). After that all the columns will be a day in the corresponding month in chronological order.

The content should be an X when they pressed the GC button. Otherwise the cell should be empty.

My problem

I have no clue how to get the dates as columns while keeping the binding with the employees. I've already searched a lot on exporting of the model and of tables but nothing is actually near to what I need.

If anyone has some experience or done this before I would be really gratefull for some help.

Thanks in advance

Upvotes: 1

Views: 1283

Answers (1)

jasbir
jasbir

Reputation: 216

Hi you can use the following libraries

'sap/ui/core/util/Export',
'sap/ui/core/util/ExportTypeCSV',

This is the sample code you can refer to suit your needs

generateExcel: function(oData, that) {
            var oModel = new JSONModel();
            oModel.setData(oData); //oData is the model data which is binding to the table

            var oTable = this.getViewById("yourTableName").getTable();
            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 Export({
                // Type that will be used to generate the content. Own ExportType’s can be created to support other formats
                exportType: new ExportTypeCSV({
                    separatorChar: ",",
                    charset: "utf-8"
                }),
                // Pass in the model created above
                models: oModel,
                // binding information for the rows aggregation
                rows: {
                    path: "/results"
                },
                // column definitions with column name and binding info for the content
                columns: aTemplate
            });
            oExport.saveFile().always(function() {
                this.destroy();
            });
        }

Hi you can use custom formatter for columns depending on types like below an example

var oColumn = {
                        name: aColumns[i].getHeader().getText(),
                        template: {
                            content: {
                                path: null,
                                formatter: function(value) {
                                    if (value instanceof(Date)) {
                                        //Convert to user date format
                                        var oFormat = DateFormat.getDateInstance({
                                            style: "short"
                                        });
                                        value = oFormat.format(value);

                                    } else {

                                        value = (value === null) ? "" : value;

                                    }
                                    return value;
                                }
                            }
                        }
                    };

Upvotes: 2

Related Questions