We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the row height for the exported excel sheet in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: October 24, 2018 |
Last Revised Date: January 12, 2020

Change the row height

You can change the row height of the exported excel sheet using Worksheets.UsedRange.RowHeight property.

C#

private void ExportToExcel_Click(object sender, System.EventArgs e)
{
    ExcelExportingOptions options = new ExcelExportingOptions();
    var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options);
    var workBook = excelEngine.Excel.Workbooks[0];
    
    //Set row height.
    workBook.Worksheets[0].UsedRange.RowHeight = 30;
 
    SaveFileDialog sfd = new SaveFileDialog
    {
        FilterIndex = 2,
        Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx",
        FileName = "Book1"
    };
 
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        using (Stream stream = sfd.OpenFile())
        {
            if (sfd.FilterIndex == 1)
                workBook.Version = ExcelVersion.Excel97to2003;
            else
                workBook.Version = ExcelVersion.Excel2010;
            workBook.SaveAs(stream);
        }
 
        //Message box confirmation to view the created spreadsheet.
        if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
            System.Diagnostics.Process.Start(sfd.FileName);
        }
    }
}

VB

Private Sub ExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
    Dim options As New ExcelExportingOptions()
    Dim excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options)
    Dim workBook = excelEngine.Excel.Workbooks(0)
       
    'Set row height.
    workBook.Worksheets(0).UsedRange.RowHeight = 30
 
    Dim sfd As SaveFileDialog = New SaveFileDialog With {.FilterIndex = 2, .Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", .FileName = "Book1"}
 
    If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
       Using stream As Stream = sfd.OpenFile()
       If sfd.FilterIndex = 1 Then
          workBook.Version = ExcelVersion.Excel97to2003
       Else
          workBook.Version = ExcelVersion.Excel2010
       End If
           workBook.SaveAs(stream)
       End Using
 
       'Message box confirmation to view the created spreadsheet.
       If MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) = System.Windows.Forms.DialogResult.OK Then
          'Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
          System.Diagnostics.Process.Start(sfd.FileName)
       End If
    End If
End Sub

 

Show the change row height for the exported excel sheet
You can use AutofitRows method to adjust the row height of the exported excel sheet based on the content.

C#

private void ExportToExcel_Click(object sender, System.EventArgs e)
{
    ExcelExportingOptions options = new ExcelExportingOptions();
    var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options);
    var workBook = excelEngine.Excel.Workbooks[0];
 
    //Row height will be set based on the content.
    workBook.Worksheets[0].UsedRange.AutofitRows();
 
    SaveFileDialog sfd = new SaveFileDialog
    {
        FilterIndex = 2,
        Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx",
        FileName = "Book1"
    };
 
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        using (Stream stream = sfd.OpenFile())
        {
            if (sfd.FilterIndex == 1)
                workBook.Version = ExcelVersion.Excel97to2003;
            else
                workBook.Version = ExcelVersion.Excel2010;
            workBook.SaveAs(stream);
        }
 
        //Message box confirmation to view the created spreadsheet.
        if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
            System.Diagnostics.Process.Start(sfd.FileName);
        }
    }
}

VB

Private Sub ExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
    Dim options As New ExcelExportingOptions()
    Dim excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options)
    Dim workBook = excelEngine.Excel.Workbooks(0)
 
    'Row height will be set based on the content.
    workBook.Worksheets(0).UsedRange.AutofitRows()
 
    Dim sfd As SaveFileDialog = New SaveFileDialog With {.FilterIndex = 2, .Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", .FileName = "Book1"}
 
     If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Using stream As Stream = sfd.OpenFile()
 If sfd.FilterIndex = 1 Then
    workBook.Version = ExcelVersion.Excel97to2003
 Else
    workBook.Version = ExcelVersion.Excel2010
 End If
              workBook.SaveAs(stream)
        End Using
 
        'Message box confirmation to view the created spreadsheet.
        If MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) = System.Windows.Forms.DialogResult.OK Then
           'Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
           System.Diagnostics.Process.Start(sfd.FileName)
        End If
     End If
End Sub

 

Show the change row height for the exported excel sheet

Sample: How to change the row height for the exported excel sheet?

Reference link: https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile