Hi,
Im try to export a SFGrid whit some columns getting the data with template.
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; }
}
|
<SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Orders" Toolbar="@(new List<string>() { "ExcelExport" })" AllowExcelExport="true" AllowPaging="true">
<GridEvents OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents>
<GridColumns>
<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);
}
}
} |
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;
}
}