How to Auto Resize Rows and Columns in Excel

In this tutorial, we explore the process of auto resizing rows and columns in Excel using the IronXL library in C#. To start, ensure you have the latest version of IronXL installed via the NuGet Package Manager for optimal performance. Import the IronXL namespace, then load an existing Excel spreadsheet containing data, such as employee records. Begin by using the auto resize row method to adjust the height of Row 1 to fit its content. Similarly, apply the auto resize column method to Column A to ensure the column width matches the content's size. Save these adjustments in a new Excel file named 'autoresize.xlsx'. Upon execution, you'll notice the enhanced readability and organization of the resized spreadsheet, with unnecessary white space and clipped text eliminated, offering a cleaner presentation compared to the original file. This method provides an efficient way to optimize Excel spreadsheets without manual adjustments, improving both functionality and aesthetics. We encourage you to try a trial subscription of the software and leave any questions or feedback in the comments.

// Make sure to include the necessary namespaces
using IronXL;

class ExcelAutoResizeExample
{
    static void Main()
    {
        // Load an existing Excel workbook
        WorkBook workbook = WorkBook.Load("EmployeeRecords.xlsx");

        // Get the first worksheet from the workbook
        WorkSheet sheet = workbook.WorkSheets.First();

        // Auto resize the first row to fit its content
        sheet.Rows["1"].AutoFit();

        // Auto resize the first column to fit its content
        sheet.Columns["A"].AutoFit();

        // Save the adjustments in a new Excel file
        workbook.SaveAs("autoresize.xlsx");
    }
}
// Make sure to include the necessary namespaces
using IronXL;

class ExcelAutoResizeExample
{
    static void Main()
    {
        // Load an existing Excel workbook
        WorkBook workbook = WorkBook.Load("EmployeeRecords.xlsx");

        // Get the first worksheet from the workbook
        WorkSheet sheet = workbook.WorkSheets.First();

        // Auto resize the first row to fit its content
        sheet.Rows["1"].AutoFit();

        // Auto resize the first column to fit its content
        sheet.Columns["A"].AutoFit();

        // Save the adjustments in a new Excel file
        workbook.SaveAs("autoresize.xlsx");
    }
}
' Make sure to include the necessary namespaces
Imports IronXL

Friend Class ExcelAutoResizeExample
	Shared Sub Main()
		' Load an existing Excel workbook
		Dim workbook As WorkBook = WorkBook.Load("EmployeeRecords.xlsx")

		' Get the first worksheet from the workbook
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' Auto resize the first row to fit its content
		sheet.Rows("1").AutoFit()

		' Auto resize the first column to fit its content
		sheet.Columns("A").AutoFit()

		' Save the adjustments in a new Excel file
		workbook.SaveAs("autoresize.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Explanation:

  • The using IronXL; statement at the top imports the IronXL library, allowing us to access its functionalities for Excel manipulation.
  • The WorkBook class is used to load an existing Excel file (EmployeeRecords.xlsx) into C#.
  • WorkSheet sheet = workbook.WorkSheets.First(); accesses the first worksheet in the loaded workbook.
  • sheet.Rows["1"].AutoFit(); automatically adjusts the height of Row 1 to fit its content.
  • sheet.Columns["A"].AutoFit(); automatically adjusts the width of Column A to fit its content.
  • Finally, workbook.SaveAs("autoresize.xlsx"); saves the modified workbook with the new auto-resized rows and columns as autoresize.xlsx.

Further Reading: How to Auto Resize Rows and Columns

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Set Cell Background Patterns and Color in Excel
NEXT >
How to Add Rows and Columns in Excel as a C# Developer