Auto-Resize Excel Rows and Columns in C#

Resizing rows and columns in a spreadsheet can save a lot of space and make it more readable. The IronXL C# library has the functionality to auto resize rows and columns. Since it is done in C#, the resize methods can be called upon all the existing rows and columns, thereby automating the manual tasks in spreadsheets.

Auto Resize Rows

The AutoSizeRow method automatically resizes the height of the specified row based on its value length. Another overload of AutoSizeRow takes the second parameter as a Boolean value. If true, the height of merged cells will be considered.

Here is a sample usage in C#:

// Import the IronXL namespace
using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing spreadsheet.
        WorkBook workbook = WorkBook.Load("example.xlsx");
        WorkSheet sheet = workbook.WorkSheets.First();

        // AutoSizeRow to automatically adjust the height of the first row (index 0)
        sheet.AutoSizeRow(0);

        // AutoSizeRow considering the height of merged cells in the second row (index 1)
        sheet.AutoSizeRow(1, true);

        // Save changes
        workbook.SaveAs("resized_example.xlsx");
    }
}
// Import the IronXL namespace
using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing spreadsheet.
        WorkBook workbook = WorkBook.Load("example.xlsx");
        WorkSheet sheet = workbook.WorkSheets.First();

        // AutoSizeRow to automatically adjust the height of the first row (index 0)
        sheet.AutoSizeRow(0);

        // AutoSizeRow considering the height of merged cells in the second row (index 1)
        sheet.AutoSizeRow(1, true);

        // Save changes
        workbook.SaveAs("resized_example.xlsx");
    }
}
' Import the IronXL namespace
Imports IronXL

Friend Class Program
	Shared Sub Main()
		' Load the existing spreadsheet.
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' AutoSizeRow to automatically adjust the height of the first row (index 0)
		sheet.AutoSizeRow(0)

		' AutoSizeRow considering the height of merged cells in the second row (index 1)
		sheet.AutoSizeRow(1, True)

		' Save changes
		workbook.SaveAs("resized_example.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Auto Resize Columns

Use the AutoSizeColumn method to resize the width of a column based on its value length. Similar to AutoSizeRow, it is also possible to make the resizing of the column consider the width of a merged cell.

Here is how you can use it in C#:

// Import the IronXL namespace
using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing spreadsheet.
        WorkBook workbook = WorkBook.Load("example.xlsx");
        WorkSheet sheet = workbook.WorkSheets.First();

        // AutoSizeColumn to automatically adjust the width of the first column (index 0)
        sheet.AutoSizeColumn(0);

        // AutoSizeColumn considering the width of merged cells in the second column (index 1)
        sheet.AutoSizeColumn(1, true);

        // Save changes
        workbook.SaveAs("resized_example.xlsx");
    }
}
// Import the IronXL namespace
using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing spreadsheet.
        WorkBook workbook = WorkBook.Load("example.xlsx");
        WorkSheet sheet = workbook.WorkSheets.First();

        // AutoSizeColumn to automatically adjust the width of the first column (index 0)
        sheet.AutoSizeColumn(0);

        // AutoSizeColumn considering the width of merged cells in the second column (index 1)
        sheet.AutoSizeColumn(1, true);

        // Save changes
        workbook.SaveAs("resized_example.xlsx");
    }
}
' Import the IronXL namespace
Imports IronXL

Friend Class Program
	Shared Sub Main()
		' Load the existing spreadsheet.
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' AutoSizeColumn to automatically adjust the width of the first column (index 0)
		sheet.AutoSizeColumn(0)

		' AutoSizeColumn considering the width of merged cells in the second column (index 1)
		sheet.AutoSizeColumn(1, True)

		' Save changes
		workbook.SaveAs("resized_example.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Please note that all the row and column positions mentioned above follow zero-based indexing.