导出到树结构化表的Excel

时间:2017-05-12 20:20:31

标签: javascript sapui5

Hello社区成员,

我正在为其中一个以表格格式提供树状结构数据的SAP Fiori应用程序实现导出到Excel功能。

Tree Table

我尝试使用ExportTypeCSV方法实现,但它以线性格式显示,没有树结构。

以下是代码。

exportToExcel: function(table, oModel)
{
        var cols = table.getColumns();      
        var items = table.getRows();

         var cellId = null;     
         var cellObj = null;        
         var cellVal = null;        
         var headerColId = null;        
         var headerColObj = null;       
         var headerColVal = null;       
         var column = null;

         var json = {}; var colArray = []; var itemsArray= [];

         //push header column names to array        
         for(var j=0; j<cols.length;j++){

                column = "";            
                column = cols[j];

                headerColId = column.getId();       
                headerColObj = sap.ui.getCore().byId(headerColId);      
                headerColVal = headerColObj.getLabel().getText();

                if(headerColObj.getVisible()){      
                    json={name: headerColVal};      
                    colArray.push(json);

                }       
            }       
            itemsArray.push(colArray);

          //push table cell values to array

          for (var i = 0; i < items.length; i++) {      
              colArray = [];        
              cellId = "";   cellObj = "";  cellVal = "";       
              headerColId = null; headerColObj = null; headerColVal = null;

              var item = items[i];

                for(var j=0; j<cols.length;j++){        
                    cellId = item.getAggregation("cells")[j].getId();       
                    cellObj = sap.ui.getCore().byId(cellId);

                    if(cellObj.getVisible()){

                        if(cellObj instanceof sap.m.Text ||cellObj instanceof sap.m.Label ||cellObj instanceof sap.m.Link) {                                    
                            cellVal = cellObj.getText();
                        }

                        if(cellObj instanceof sap.m.HBox){                                  
                            cellVal = cellObj.getItems()[0].getText();
                        }

                        if(cellObj instanceof sap.m.ObjectNumber){                                  
                            var k = cellObj.getUnit();      
                            cellVal = cellObj.getNumber()+" "+k;

                        }

                        if(cellObj instanceof sap.m.ObjectIdentifier){                                  
                            var objectIdentifierVal = "";

                            if(cellObj.getTitle() != undefined && cellObj.getTitle() != "" && cellObj.getTitle() != null )      
                                objectIdentifierVal = cellObj.getTitle();

                            if(cellObj.getText() != undefined && cellObj.getText() != "" && cellObj.getText() != null )     
                                objectIdentifierVal = objectIdentifierVal+" "+cellObj.getText();        

                            cellVal = objectIdentifierVal;      
                        }       
                        if(cellObj instanceof sap.ui.core.Icon){                                    
                            if(cellObj.getTooltip() != undefined && cellObj.getTooltip() != "" && cellObj.getTooltip() != null )        
                            cellVal = cellObj.getTooltip();     
                        }       
                        if(j==0){       
                            json={ name:  "\r"+cellVal};        
                        }       
                        else        
                        {       
                            json={ name:  cellVal};     
                        }       
                        colArray.push(json);        
                    }       
                }       
                itemsArray.push(colArray);
            }

         //export json array to csv file

          var oExport = new sap.ui.core.util.Export({

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

                    separatorChar: ","

                }),                     

                models: oModel,

                rows: {     
                    path: "/"       
                },

                // column definitions with column name and binding info for the content     
                columns: [itemsArray]       
            });

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

在excel中实现树类型格式化的任何指针都非常有用。

谢谢,

SAURABH

0 个答案:

没有答案