Adjust Row Height and Column Width in Excel using C#

In certain cases, you have to adjust the height of the rows and the width of the columns according to the content in Excel files. To achieve this, MS Excel provides Cell Width and Row Height options in the Format menu of the Cells group. However, how would you do it while working with Excel files programmatically in C#? In this article, you will learn how to adjust row height and column width in Excel files using C#.

C# API to Adjust Excel Row Height and Column Width

Aspose.Cells for .NET lets you create and manipulate Excel files seamlessly from within your .NET applications. We will use this API to process our Excel files and set the height of rows and width of columns. You can either download the API or install it from NuGet.

PM> Install-Package Aspose.Cells

Adjust Row’s Height in Excel in C#

The following are the steps to adjust a row’s height in an Excel file in C#.

  • First, use Workbook class to load the Excel file.
  • Then, use Workbook.Worksheets collection to access the desired worksheet by index.
  • Get the reference of cells in the worksheet using Worksheet.Cells collection.
  • Call SetRowHeight() method and pass rows’s index and height as parameters.
  • Finally, save the updated Excel file using Workbook.Save(string) method.

The following code sample shows how to set the row’s height in Excel in C#.

The following screenshot shows the Excel worksheet after setting the row’s height.

Adjust Row Height in Excel using C#

Set Column’s Width in Excel in C#

The following are the steps to set a column’s width in an Excel file in C#.

  • First, use Workbook class to load the Excel file.
  • Then, use Workbook.Worksheets collection to access the desired worksheet by index.
  • Get the reference of cells using Worksheet.Cells collection.
  • Call SetColumnWidth() method and pass column’s index and width as parameters.
  • Finally, save the updated Excel file using Workbook.Save(string) method.

The following code sample shows how to set a column’s width in an Excel file in C#.

The following is the screenshot of the Excel worksheet after adjusting the column’s width.

Adjust Column Width in Excel using C#

C# API to Set Excel Row Height and Column Width - Get a Free License

You can get a free temporary license to set the row height and column width in Excel files without evaluation limitations.

C# .NET Excel API - Explore More

You can explore more about the C# Excel API using the documentation. Also, you can have a look at the API references here.

Conclusion

In this article, you have learned how to adjust rows’ height and columns’ width in Excel files using C#. The code samples and screenshots have demonstrated how to set a particular height for a row and width for a column in an Excel file.

Have a Question?

In case you would have any questions or queries, you can share them with us via our forum.

See Also