Introduction
When we work with spreadsheets and add data to rows or columns, we might need to increase or decrease the height of the rows or width of the columns. Sometimes, just because of applying some formatting settings on the rows or columns, we also may need to do so. Normally, you adjust your row heights and column widths in a WYSIWYG environment using Microsoft Excel. But, Aspose.Cells gives you the power to perform these operations at runtime.
Adjusting Row Height
Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a Worksheets collection that allows to access each worksheet in the Excel file. A worksheet is represented by the Worksheet class. Worksheet class provides a Cells collection that represents all cells in the worksheet.
Cells collection provides several methods to manage rows or columns in a worksheet, few of these are discussed below in more detail.
Setting the Height of a Row
Developers can set the height of a single row by calling SetRowHeight method of the Cells collection. SetRowHeight method takes two parameters as follows:
- Row Index, represents the index of the row whose height is needed to change
- Row Height, represents the desired row height to apply on the row
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("C:\\book1.xls",FileMode.Open);
//Opening the Excel file through the file stream
workbook.Open(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Setting the height of the second row to 13
worksheet.Cells.SetRowHeight(1, 13);
//Saving the modified Excel file in default (that is Excel 2003) format
workbook.Save("C:\\output.xls",FileFormatType.Default);
//Closing the file stream to free all resources
fstream.Close();
[VB.NET]
'Instantiating a Excel object
Dim workbook As Excel = New Workbook()
'Creating a file stream containing the Excel file to be opened
Dim fstream As FileStream = New FileStream("C:\\book1.xls",FileMode.Open)
'Opening the Excel file through the file stream
workbook.Open(fstream)
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Setting the height of the second row to 13
worksheet.Cells.SetRowHeight(1, 13)
'Saving the modified Excel file in default (that is Excel 2003) format
workbook.Save("C:\\output.xls",FileFormatType.Default)
'Closing the file stream to free all resources
fstream.Close()
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Opening the Excel file
workbook.open("C:\\book1.xls");
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().getSheet(0);
Cells cells = worksheet.getCells();
//Setting the height of the second row to 13
cells.setRowHeight(1, 13);
//Saving the modified Excel file in default (that is Excel 2003) format
workbook.save("C:\\output.xls",FileFormatType.DEFAULT);
Setting the Height of All Rows in a Worksheet
If developers need to set the same row height for all rows in the worksheet, they can do it by using the StandardHeight property of the Cells collection.
Example:
[C#]
//Setting the height of all rows in the worksheet to 15
worksheet.Cells.StandardHeight = 15;
[VB.NET]
'Setting the height of all rows in the worksheet to 15
worksheet.Cells.StandardHeight = 15
[JAVA]
//Setting the height of all rows in the worksheet to 15
worksheet.Cells.setStandardHeight(15f);
Adjusting Column Width
Setting the Width of a Column
Developers can set the width of a column by calling SetColumnWidth method of the Cells collection. SetColumnWidth method takes two parameters as follows:
- Column Index, represents the index of the column whose width is needed to change
- Column Width, represents the desired column width to apply on the column
Example:
[C#]
//Setting the width of the second column to 17.5
worksheet.Cells.SetColumnWidth(1, 17.5);
[VB.NET]
'Setting the width of the second column to 17.5
worksheet.Cells.SetColumnWidth(1, 17.5)
[JAVA]
//Setting the width of the second column to 17.5
cells.setColumnWidth(1, 17.5);
Setting the Width of All Columns in a Worksheet
If developers need to set the same column width for all columns in the worksheet, they can do it by using the StandardWidth property of the Cells collection.
Example:
[C#]
//Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.StandardWidth = 20.5;
[VB.NET]
'Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.StandardWidth = 20.5
[JAVA]
//Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.setStandardWidth(20.5f);