I want to remove the word "Sum", and only keep the number at the footer of the Excel file when I export data from the grid as pictures below. Is there anyone know? Please help me.
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx
@using Telerik.Windows.Documents.Spreadsheet.Model
<TelerikGrid Data="@GridData">
<GridExport><GridExcelExportOnAfterExport="@OnGridAfterExport" /></GridExport><GridAggregates><GridAggregateField="@nameof(Product.Name)"Aggregate="@GridAggregateType.Count" /><GridAggregateField="@nameof(Product.Stock)"Aggregate="@GridAggregateType.Sum" /></GridAggregates><GridToolBarTemplate><GridCommandButtonCommand="ExcelExport">Export to Excel</GridCommandButton></GridToolBarTemplate><GridColumns><GridColumnField="@nameof(Product.Name)"><FooterTemplate>
@context.Count
</FooterTemplate></GridColumn><GridColumnField="@nameof(Product.Stock)"><FooterTemplate>
@context.Sum
</FooterTemplate></GridColumn></GridColumns>
</TelerikGrid>
@code {
List<Product> GridData { get; set; } = new List<Product>();
async Task OnGridAfterExport(GridAfterExcelExportEventArgs args)
{
//args.Stream is finalized. XlsxFormatProvider Import() requires a readable stream,//so copy the stream bytes to a new MemoryStream instance which will be used for the import.var bytes = args.Stream.ToArray();
var excelStream = new MemoryStream(bytes);
//create a format provider instance to call the import
XlsxFormatProvider formatProvider = new XlsxFormatProvider();
//import the stream to a workbook
Workbook workbook = formatProvider.Import(excelStream);
//get Stock sum cell and remove "Sum: "
CellSelection sumCell = workbook.Worksheets[0].Cells[new CellIndex(GridData.Count + 1, 1)];
sumCell.SetValue(sumCell.GetValue().Value.RawValue.Replace("Sum: ", ""));
//save the modified workbook in a MemoryStream
MemoryStream modifiedExportStream = new MemoryStream();
formatProvider.Export(workbook, modifiedExportStream);
//pass the modified stream to the event arguments
args.Stream = modifiedExportStream;
}
protected override voidOnInitialized()
{
GridData = new List<Product>();
var rnd = new Random();
for (int i = 1; i <= 3; i++)
{
GridData.Add(newProduct()
{
Id = i,
Name = $"Product {i}",
Stock = rnd.Next(0, 10)
});
}
}
public classProduct{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public int Stock { get; set; }
}
}
Regards,
Dimo
Progress Telerik
As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.