This article covers how to auto fit Excel using C#. It discusses the IDE configuration, a series of steps, and a code snippet to auto fit in Excel using C#. We will also learn to autofit a specific row or a column in the Excel worksheet.
Steps to Autofit Rows and Columns in Excel using C#
- Prepare the IDE to use Aspose.Cells for .NET to autofit rows and columns
- Load the input file with the Workbook class
- Access any sheet using the worksheet class object with its index or name
- Autofit the rows and columns
- Export the output workbook with the Save method
These steps summarize how to auto size cells in Excel using C#. You need to access the specific sheet using the zero-based index or by specifying the worksheet name. Subsequently, autofit the rows and columns before exporting the output Excel file.
Code to Autofit Rows and Columns in Excel using C#
This sample code presents the simple process to autofit Excel columns using C#. Likewise, it resizes the row height to adjust the cell contents within a cell. However, you can also work with overload methods like AutoFitRow(index), AutoFitColumn(index) where the row and column indexes are zero-based numbers to process a specific row or column based on your requirements.
This article has elaborated on autofit rows using C#. To wrap text on the cell level, read the article on how to wrap text in Excel using C#.