diff --git a/grid-sdk/blazor/data-grid/excel-export-options.md b/grid-sdk/blazor/data-grid/excel-export-options.md index 4e439b1b..27c0a79d 100644 --- a/grid-sdk/blazor/data-grid/excel-export-options.md +++ b/grid-sdk/blazor/data-grid/excel-export-options.md @@ -1896,6 +1896,119 @@ This method is helpful when the exported **Excel** file needs to support data an } +{% endhighlight %} + +{% highlight c# tabtitle="Javascript.js" %} + +function saveAsFile(filename, bytesBase64) { + var link = document.createElement('a'); + link.download = filename; + link.href = "data:application/octet-stream;base64," + bytesBase64; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); +} + +{% endhighlight %} +{% endtabs %} + +### Change row height in exported file + +The Blazor Data Grid can export data as a memory stream, allowing modification of the Excel workbook before the exported file is delivered to the client. Using the [XlsIO](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core/) library, worksheet formatting can be customized programmatically during the export process. + +The [SetRowHeightInPixels](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IWorksheet.html#Syncfusion_XlsIO_IWorksheet_SetRowHeightInPixels_System_Int32_System_Double_) method is used to modify the height of a specific row in the exported Excel file by providing the row index and the required height in pixels. This enables row-level customization in the exported workbook. + +In the following example, an [SfNumericTextBox](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Inputs.SfNumericTextBox-1.html) component is used to specify the Excel row index dynamically. The selected value is updated through the [ValueChange](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Inputs.NumericTextBoxEvents-1.html#Syncfusion_Blazor_Inputs_NumericTextBoxEvents_1_ValueChange) event and stored in the NumericValue property. During the Excel export operation, the value of NumericValue is passed to the `SetRowHeightInPixels` method, allowing the height of the corresponding row in the exported Excel document to be modified. In this sample, the height of the specified row is set to **100** pixels. + +{% tabs %} +{% highlight razor tabtitle="Index.razor" %} + +@using Syncfusion.Blazor.Grids +@using System.IO +@using Syncfusion.XlsIO +@inject IJSRuntime JSRuntime +@using Syncfusion.Blazor.Inputs + +
+ + + + +
+
+ + + + + + + + + + +@code +{ + private SfGrid DefaultGrid; + public List Orders { get; set; } + public int NumericValue { get; set; } = 1; + + protected override void OnInitialized() + { + Orders = Enumerable.Range(1, 9).Select(x => new Order() + { + OrderID = x, + CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)], + Freight = 2.1 * x, + }).ToList(); + } + + public void OnChange(Syncfusion.Blazor.Inputs.ChangeEventArgs args) + { + if(args.Value != 0) + { + NumericValue = args.Value; + StateHasChanged(); + } + } + + public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args) + { + if (args.Item.Id == "Grid_excelexport") + { + ExcelExportProperties ExportProperties = new ExcelExportProperties(); + + using var stream = await DefaultGrid.ExportToExcelAsync(asMemoryStream: true, ExportProperties); + + var copyOfStream = new MemoryStream(stream.ToArray()); + + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(copyOfStream); + IWorksheet worksheet = workbook.Worksheets[0]; + + // Set the height of the specified row in the exported Excel file. + worksheet.SetRowHeightInPixels(NumericValue, 100.0); + + using (MemoryStream outputStream = new MemoryStream()) + { + workbook.SaveAs(outputStream); + await JSRuntime.InvokeVoidAsync("saveAsFile", "GridExport.xlsx", Convert.ToBase64String(outputStream.ToArray())); + } + } + } + } + + public class Order + { + public int? OrderID { get; set; } + public string CustomerID { get; set; } + public double? Freight { get; set; } + } +} + {% endhighlight %} {% highlight c# tabtitle="Javascript.js" %}