Articles in this section
Category / Section

How to export the WinUI DataGrid (SfDataGrid) to excel with RowHeader?

3 mins read

WinUI DataGrid (SfDataGrid) does not provide the support to export the row header. You can export the row header column by using Worksheet.InsertColumn method to insert a column manually in ExcelSheet and make customization in the inserted column.

private void btnExportToExcelClicked(object sender, RoutedEventArgs e)
{            
    var options = new DataGridExcelExportOptions(); 
    var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options);
    var workBook = excelEngine.Excel.Workbooks[0];
    IWorksheet sheet = workBook.Worksheets[0];

    // Inserting a new column to the left of the first column
    sheet.InsertColumn(1, 1, ExcelInsertOptions.FormatDefault);
    
    // Get the number of rows count
    var rowcount = this.dataGrid.RowGenerator.Items.Count;

    for (int i = 1; i < rowcount; i++)
    {
        // Inserting the row index in the first row
        sheet.Range["A" + (i + 1).ToString()].Number = i;
    }

    MemoryStream stream = new MemoryStream();
    workBook.SaveAs(stream);
    Save(stream, "Sample");

    async void Save(MemoryStream stream, string filename)
    {
        StorageFile stFile;

        if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
        {
            FileSavePicker savePicker = new FileSavePicker();
            savePicker.DefaultFileExtension = ".xlsx";
            savePicker.SuggestedFileName = filename;
            savePicker.FileTypeChoices.Add("Excel Documents", new List<string>() { ".xlsx" });
            var hwnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hwnd);
            stFile = await savePicker.PickSaveFileAsync();
        }
        else
        {
            StorageFolder local = ApplicationData.Current.LocalFolder;
            stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
        }
        if (stFile != null)
        {
            using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                //Write the compressed data from the memory to the file
                using (Stream outstream = zipStream.AsStreamForWrite())
                {
                    byte[] buffer = stream.ToArray();
                    outstream.Write(buffer, 0, buffer.Length);
                    outstream.Flush();
                }
            }
            //Launch the saved Excel file.
            await Windows.System.Launcher.LaunchFileAsync(stFile);
        }
    }
}

The following screenshot illustrates the RowHeader displayed in WinUI DataGrid (SfDataGrid),

CustomizationrowheaderDataGrid.png

The following screenshot illustrates exported Excel Sheet with RowHeader,

ExcelDisplayRowHeader.png

Take a moment to peruse the WinUI DataGrid - Export To Excel documentation, where you can find about export to Excel with code examples.

View Sample in GitHub

Conclusion
I hope you enjoyed learning about how to export the WinUI DataGrid (SfDataGrid) to excel with RowHeader?

You can refer to our WinUI DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinUI DataGrid documentation to understand how to present and manipulate data.

For current customers, you can check out our WinUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinUI DataGrid and other WinUI components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied