How to Adjust Row Height in Excel using C#

This quick tutorial will explain how to adjust row height in Excel using C#. First, you will load the sample workbook and then adjust the height of the individual row or multiple rows to a fixed height or based on contents in the rows as per your requirement. After you change Excel row height using C#, you can save it back on the disk in any format like XLSX.

Steps to Adjust Row Height in Excel using C#

  1. Using NuGet package manager, add Aspose.Cells to adjust the height of the rows
  2. Add a reference to Aspose.Cells namespace to use API calls in the code
  3. Load the sample workbook containing multiple worksheets to test the feature
  4. Get a reference of the desired worksheet to adjust the rows’ height
  5. Set the height of the rows to a fixed value or based on contents as per the requirement
  6. Save the workbook with specified rows height

In these simple steps, we first load the target Excel file into the Workbook class object and then access the reference of the desired worksheet to adjust Excel row height using C#. We have full control over the process as we can change height in a variety of ways like changing the height of a complete single row, selected columns of a row, a range of rows, or the entire worksheet.

Code to Adjust Excel Row Height using c#

In this sample code, we access the worksheet’s cells collection and then set the individual row’s height using the Cells.SetRowHeight() function. Next, we have explained the adjustment of row height based on the contents by providing the range of columns within a row using AutoFitRow() function and then adjusted multiple rows by providing a range of rows to AutoFitRows() function. In the last, we have set the height of all the rows in a worksheet by calling AutoFitRows() function without any arguments.

With the help of this short tutorial, you have explored how to change rows height. If you want to learn other features like creating charts, refer to the article on how to create Excel Pie chart in C#.

 English