How to Auto Resize Excel Rows & Columns in C#
IronXL provides automated row and column resizing in C# with methods like AutoSizeRow() and AutoSizeColumn() that adjust dimensions based on cell content, eliminating manual spreadsheet formatting and supporting merged cell calculations. This feature automates Excel workflows by dynamically adapting cell dimensions to their content, ensuring professional spreadsheets without manual intervention.
Quickstart: Auto-Resize a Column or Row with a Single Call
With IronXL, you can resize any row or column in one line. Use WorkSheet.AutoSizeColumn(int, bool) or WorkSheet.AutoSizeRow(int, bool) to adjust dimensions based on cell content (merged cells optional). This approach works across all .NET platforms without requiring Excel installation.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
// One-line: auto-resize column A including merged cells workSheet.AutoSizeColumn(0, true); // Or auto-resize row 5 quickly workSheet.AutoSizeRow(4, true);Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library to auto resize rows and columns in Excel
- Import the desired Excel file
- Utilize the
AutoSizeRowmethod to automatically resize rows - Use the
AutoSizeColumnmethod to resize columns based on their content width - Set the height and width values to your requirements
How Do I Auto Resize Rows?
The AutoSizeRow method automatically resizes the height of specified rows based on their content. This feature ensures all text within cells is visible, preventing truncation and improving readability. When working with Excel files without Interop, IronXL calculates optimal row height based on font size, cell content, and formatting attributes.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-rows.csusing IronXL;
// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Apply auto resize on row 2
workSheet.AutoSizeRow(1);
workBook.SaveAs("autoResize.xlsx");Imports IronXL
' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply auto resize on row 2
workSheet.AutoSizeRow(1)
workBook.SaveAs("autoResize.xlsx")The auto-resize algorithm considers font metrics, cell padding, and text wrapping settings to determine optimal height. For developers migrating from traditional Excel automation, this provides an efficient alternative that doesn't require Excel installation.
What Does the Result Look Like?

How Do I Auto Resize Columns?
Use the AutoSizeColumn method to resize column width based on content length. This method analyzes the longest content in each column and adjusts width accordingly, ensuring all data is visible without horizontal scrolling. The algorithm considers text length, number formatting, date displays, and other cell formatting that affects required width.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-advance-rows.csusing IronXL;
// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Apply auto resize to rows individually
workSheet.AutoSizeRow(0, true);
workSheet.AutoSizeRow(1, true);
workSheet.AutoSizeRow(2, true);
workBook.SaveAs("advanceAutoResizeRow.xlsx");Imports IronXL
' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply auto resize to rows individually
workSheet.AutoSizeRow(0, True)
workSheet.AutoSizeRow(1, True)
workSheet.AutoSizeRow(2, True)
workBook.SaveAs("advanceAutoResizeRow.xlsx")When working with financial data or reports, properly sized columns are crucial for readability. IronXL's auto-resize functionality integrates with features like cell borders and alignment to create professional spreadsheets.
What Does the Result Look Like?

How Do I Handle Advanced Row Resizing with Merged Cells?
Another overload of the AutoSizeRow method takes a second Boolean parameter. This parameter lets you consider merged cells when resizing. When working with merged cells in Excel, auto-resize behavior becomes more complex as content spans multiple cells.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-advance-rows.csusing IronXL;
// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Apply auto resize to rows individually
workSheet.AutoSizeRow(0, true);
workSheet.AutoSizeRow(1, true);
workSheet.AutoSizeRow(2, true);
workBook.SaveAs("advanceAutoResizeRow.xlsx");Imports IronXL
' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply auto resize to rows individually
workSheet.AutoSizeRow(0, True)
workSheet.AutoSizeRow(1, True)
workSheet.AutoSizeRow(2, True)
workBook.SaveAs("advanceAutoResizeRow.xlsx")How Does Merged Cell Calculation Work?
If content has a height of 192 pixels and is located in a merged region spanning 3 rows, when applying auto resize to any of these rows, the algorithm divides 192 px by 3, resulting in 64 px for each row. The AutoSizeRow method needs to be applied to each row individually. This distribution ensures merged content displays properly across all rows in the merge region.

What if the value is set to false?
When set to false, the AutoSizeRow method adjusts row height based on the cell with the highest content. In Microsoft Excel, merging cells retains only the upper-left value and clears the rest. However, IronXL's Merge operation preserves values of other cells in the merged region, providing more flexibility with complex spreadsheets.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-advance-rows-false.csusing IronXL;
workSheet.Merge("A1:A3");
workSheet.AutoSizeRow(0, false);
workSheet.AutoSizeRow(1, false);
workSheet.AutoSizeRow(2, false);Imports IronXL
workSheet.Merge("A1:A3")
workSheet.AutoSizeRow(0, False)
workSheet.AutoSizeRow(1, False)
workSheet.AutoSizeRow(2, False)
For demonstration purposes, I manually adjusted row height instead of using the Excel autofit row height function to avoid adding noticeable top and bottom padding.
With the useMergedCells parameter set to false, merged cell height is not considered. Each row's height is calculated based solely on its content. This approach maintains consistent row heights regardless of merged regions.
How Do I Handle Advanced Column Resizing with Merged Cells?
Similar to AutoSizeRow, you can make column resizing consider merged cell width. When set to true, if a merged cell has the longest content, the resized column width equals the merged cell's width divided by the number of columns in the merged region. This feature helps when creating spreadsheets with complex layouts.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-advance-columns.csusing IronXL;
// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Apply auto resize to columns individually
workSheet.AutoSizeColumn(0, true);
workSheet.AutoSizeColumn(1, true);
workSheet.AutoSizeColumn(2, true);
workBook.SaveAs("advanceAutoResizeColumn.xlsx");Imports IronXL
' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply auto resize to columns individually
workSheet.AutoSizeColumn(0, True)
workSheet.AutoSizeColumn(1, True)
workSheet.AutoSizeColumn(2, True)
workBook.SaveAs("advanceAutoResizeColumn.xlsx")How Does Merged Cell Width Calculation Work?
If content has a width of 117 pixels in a merged region spanning 2 columns, applying auto resize to any column results in 59 pixels for each column. The AutoSizeColumn method must be applied to each column individually. This proportional distribution ensures merged content displays properly across all columns in the merge region.

What if the value is set to false?
When set to false, the AutoSizeColumn method adjusts width based on the cell with the longest content. In Microsoft Excel, merging cells keeps only the upper-left value and erases the rest. However, IronXL's Merge method preserves values of other cells in the merged region, giving developers more control over their data.
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-advance-columns-false.csworkSheet.Merge("A1:B1");
workSheet.AutoSizeColumn(0, false);
workSheet.AutoSizeColumn(1, false);workSheet.Merge("A1:B1")
workSheet.AutoSizeColumn(0, False)
workSheet.AutoSizeColumn(1, False)
Merged cell width was not prioritized, resulting in no width changes because each column's width was calculated based on its content width.
What Are the Differences between Excel and IronXL Auto Resize?
Understanding differences between Excel's native auto-fit functionality and IronXL's implementation helps developers make informed decisions when automating spreadsheet operations. These differences matter when maintaining consistency across platforms or when precise control over cell dimensions is required.
How Do Rows Differ?
Excel's autofit row height function applies noticeable top and bottom padding to cells. IronXL provides precise control over row heights, calculating exact space needed for content without additional padding. This difference is significant when working with densely packed data or when precise print layouts are required.

How Do Columns Differ?
Excel's autofit column width function applies left and right padding to cells, though less noticeably. IronXL focuses on exact content width, beneficial when working with automated reporting systems where space optimization is crucial.

How Do I Manually Resize Height and Width?
Besides using AutoSizeRow and AutoSizeColumn methods to calculate and adjust dimensions automatically, you can manually adjust column and row width and height to meet specific requirements. Manual sizing is useful when implementing standardized report formats or working with templates requiring specific dimensions.
- Adjust height: set the
Heightproperty ofRangeRow - Adjust width: set the
Widthproperty ofRangeColumn
:path=/static-assets/excel/content-code-examples/how-to/autosize-rows-columns-manual.csusing IronXL;
// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
RangeRow row = workSheet.GetRow(0);
row.Height = 10; // Set height
RangeColumn col = workSheet.GetColumn(0);
col.Width = 10; // Set width
workBook.SaveAs("manualHeightAndWidth.xlsx");IRON VB CONVERTER ERROR developers@ironsoftware.comWhat Units Does Excel Use for Height and Width?
Height and width measurement units in Excel differ. Height is measured in 1/20 of a point, while width is based on the number of "0"s that fit in a cell using the font specified in the Normal style. When working with IronXL's API, understanding these conversions is essential for precise layouts.
To simplify unit conversion, interpret IronXL values as display pixels in Microsoft Excel. A pixel is defined as 1/96 of an inch, regardless of screen resolution. Excel is resolution-independent and doesn't rely on physical pixels.
The relationship between IronXL and Excel height and width measurements:
- Height:
RangeRow.Height = 10results in approximately1 pixel - Width:
RangeColumn.Width = 23.255corresponds to 1 pixel
For developers requiring precise spreadsheet layout control, these conversion factors enable accurate dimension calculations. Combined with other formatting features, manual sizing provides complete control over Excel document appearance. Remember to apply your license key when deploying applications using these features in production.
Frequently Asked Questions
How do I automatically resize Excel rows based on cell content in C#?
IronXL provides the AutoSizeRow method to automatically adjust row heights based on cell content. Simply call workSheet.AutoSizeRow(rowIndex, includeMergedCells) where rowIndex is the zero-based row number. The method calculates optimal height based on font size, cell content, and formatting attributes, ensuring all text is visible without truncation.
Can I auto-resize columns in Excel spreadsheets programmatically?
Yes, IronXL offers the AutoSizeColumn method to automatically adjust column widths. Use workSheet.AutoSizeColumn(columnIndex, includeMergedCells) to resize columns based on their longest content. The algorithm considers text length, number formatting, date displays, and other cell formatting to determine the optimal width.
Does auto-resizing work with merged cells?
IronXL supports auto-resizing for merged cells through the boolean parameter in AutoSizeRow and AutoSizeColumn methods. Set the second parameter to true (e.g., workSheet.AutoSizeColumn(0, true)) to include merged cell calculations when determining the appropriate dimensions.
What factors are considered when auto-resizing rows and columns?
IronXL's auto-resize algorithm considers multiple factors including font metrics, cell padding, text wrapping settings, number formatting, date displays, and cell borders. This comprehensive approach ensures professional-looking spreadsheets with properly sized cells that accommodate all content without manual intervention.
Do I need Microsoft Excel installed to use auto-resize features?
No, IronXL works independently without requiring Excel installation. The library performs all calculations internally, making it ideal for server environments and deployment scenarios where Excel cannot be installed. This approach works across all .NET platforms.
Can I auto-resize multiple rows or columns at once?
While IronXL's AutoSizeRow and AutoSizeColumn methods work on individual rows/columns, you can easily loop through multiple items to resize them. This allows batch processing of entire worksheets or specific ranges, automating Excel workflows efficiently.






