Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Code Explanation:
using IronXL;
statement at the top imports the IronXL library, allowing us to access its functionalities for Excel manipulation.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.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