简体   繁体   English

将模型导出到 csv

[英]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.这已注册到 HanaXS 数据库。 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).因此,如果决定不在办公室吃饭,他们将点击 GC 按钮(在荷兰语中代表“不消费”)。

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.我想做一个涵盖一个月的excel表。 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.当他们按下 GC 按钮时,内容应该是 X。 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提前致谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM