Excel export with template columns

Hi,

Im try to export a SFGrid whit some columns getting the data with template.


Background :
  • The columns are dynamic  (code attached - SFGrid)
  • These parameters are stored in a objects
  • A Column Template is used to extract the values from objects
  • I believe is the way to get the data, becouse we showing a object inside other, more specific, show a object list inside a object  (code attached)
Issue I'm Facing:
  • When exporting, the columns populated by a Column Template are empty, even the column name dont write on the file
Question:
  • Is it possible to configure the Grid or Export to get the data source from some custome datatable or something like that


SFGrid:

<SfGrid ID="Grid" @ref="DefaultGrid"

                    TValue="WorkOrderDto"

                    DataSource="_workOrders.Value.WorkOrders"

                    AllowFiltering="@_dashboard.Filterable"

                    AllowSorting="@_dashboard.Sortable"

                    ShowColumnChooser="true"

                    Height="100%"

                    Width="100%"

                    AllowExcelExport="true"

                    Toolbar="@(new List<string>() { "ExcelExport" })">

                <GridEvents OnToolbarClick="ToolbarClickHandler" TValue="WorkOrderDto"></GridEvents>

                <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Excel"></GridFilterSettings>

                <GridColumns>

                    @foreach (var column in _dashboard.Views.OrderBy(a => a.Order).ToList())

                    {

                        var width = column.Width > 0 ? $"{column.Width}" : "120";

                        switch (column.ViewType)

                        {

                            case ViewType.Field:

                                <GridColumn HeaderText="@column.Caption"

                                            Field="@column.ValueName"

                                            Width="@width"

                                            AllowFiltering="@column.Filterable"

                                            AllowSorting="@column.Sortable"

                                            Format="@column.Format">

                                </GridColumn>

                                break;

                            case ViewType.Category:

                                var columnName = @column.ValueName;

                                <GridColumn HeaderText="@column.Caption"

                                            Field="@column.ValueName"

                                            Width="@width"

                                            AllowFiltering="false"

                                            AllowSorting="@column.Sortable"

                                            SortComparer="new CategoryComparer(columnName)">

                                    <Template Context="data">

                                        @{

                                            var order = (data as WorkOrderDto).Categories.FirstOrDefault(a => a.Category == column.ValueName);

                                            if (order != null)

                                            {

                                                <Bubble Color="@order.Color"

                                                        Text="@order.Value?.ToString()"

                                                        HasComments="@order.Comments"

                                                        OnClick="@(() => OpenDetails((data as WorkOrderDto).Id, column.Caption, column.ReadOnly))">

                                                </Bubble>

                                            }

                                            else

                                            {

                                                <Bubble Color="grey"

                                                        Text="N/A"

                                                        HasComments="false">

                                                </Bubble>

                                            }

                                        }

                                    </Template>

                                </GridColumn>

                                break;

                        }

                    }

                </GridColumns>

            </SfGrid>


 WorkOrderDTO:

public class WorkOrderDto

    {

        public WorkOrderDto()

        {

            Categories = new List<CategoryColor>();

        }

        public int Id { get; set; }

        public string Name { get; set; }

        public int Sequence { get; set; }

        public DateTime ScheduledStartDate { get; set; }

        public DateTime ScheduledCompletionDate { get; set; }

        public DateTime CreationDate { get; set; }

        public DateTime? LineScheduleDate { get; set; }

        public decimal WipEntityId { get; set; }

        public decimal StartQuantity { get; set; }

        public decimal CompletedQuantity { get; set; }

        public decimal RemainingQuantity { get; set; }

        public string ItemName { get; set; }

        public string ItemDashboardCategoryName { get; set; }

        public string ItemPlannerCode { get; set; }

        public string ItemFamily { get; set; }

        public string ItemItemType { get; set; }

        public string StatusMeaning { get; set; }

        public int StatusId { get; set; }

        public string ItemProductHierarchyCategoryCustomName { get; set; }

        public string WorkOrderTypeName { get; set; }

        public Plant Plant { get; set; }

        public string SalesOrder { get; set; }

        public string SalesOrderLine { get; set; }

        public string CustomerName { get; set; }

        public string CustomerNumber { get; set; }


        public LineDto Line { get; set; }

        public string ClassCode { get; set; }


        public string DepartmentClassCode { get; set; }

        public bool PastDue { get; set; }

        public List<CategoryColor> Categories { get; set; }

    }


CategoryColor:

public class CategoryColor

    {

        public string Color { get; set; }

        public string Category { get; set; }

        public string Value { get; set; }

        public bool Comments { get; set; }

    }


3 Replies 1 reply marked as answer

MS Monisha Saravanan Syncfusion Team March 18, 2022 10:45 AM UTC

Hi Guillermo, 
  
Greetings from Syncfusion support. 
 
Query: “When exporting, the columns populated by a Column Template are empty, even the column name dont write on the file” 
  
We suggest you to enable the IncludeTemplateColumn property of PdfExportProperties and ExcelExportProperties to export Template columns in Grid. Kindly refer the attached code snippet for your reference. 
  
<SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Orders" Toolbar="@(new List<string>() { "ExcelExport" })" AllowExcelExport="true" AllowPaging="true"> 
    <GridEvents OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents> 
    <GridColumns>      
  <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"> 
             <Template> 
                … 
           </Template> 
        </GridColumn>        
 </GridColumns> 
</SfGrid> 
  
@code{ 
    private SfGrid<Order> DefaultGrid; 
  
    public List<Order> Orders { get; set; } 
  
    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args) 
    { 
       if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname 
        { 
            ExcelExportProperties excelExportProperties = new ExcelExportProperties(); 
            excelExportProperties.IncludeTemplateColumn = true;            
            await this.DefaultGrid.ExcelExport(excelExportProperties); 
        } 
    } 
  
} 
  
Also kindly ensure to set values for args.Cell.Value inside the corresponding event handlers(PdfQueryCellInfoEvent, ExcelQueryCellInfoEvent) to export the contents of template column in Grid for any customization on the cell. Kindly refer the attached UG for your reference. 
  
References :  
  
Kindly get back to us if you need further assistance. 
  
Regards, 
Monisha 


Marked as answer

GG Guillermo Garcia replied to Monisha Saravanan March 22, 2022 08:29 PM UTC

Thanks,

That works for me, also the documentation for assigns the values for those columns. Share you the code.


public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)

        {

            if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemName

            {

                ExcelExportProperties ExportProperties = new ExcelExportProperties();

                ExportProperties.IncludeTemplateColumn = true;

                ExportProperties.FileName = _dashboard.ViewName + ".xlsx";

                await DefaultGrid.ExcelExport(ExportProperties);

            }

        }


        public void ExcelQueryCellInfoHandler(ExcelQueryCellInfoEventArgs<WorkOrderDto> args)

        {

            var col = args.Column.HeaderText;

            CategoryColor order = _workOrders.Value.WorkOrders[args.RowIndex - 2].Categories.FirstOrDefault(c => c.Category == col);


            if (order != null)

            {

                args.Cell.Value = order.Value;

            }

        }



RS Renjith Singh Rajendran Syncfusion Team March 23, 2022 10:25 AM UTC

Hi Guillermo, 
 
Thanks for your update. We are glad to hear that you have achieved your requirement.  
 
Please get back to us if you need further assistance. 
 
Regards, 
Renjith R 


Loader.
Up arrow icon