Adjusting Row Height and Column Width
Working with Rows
Adjusting Row Height
Aspose.Cells provides a class, IWorkbook that represents a Microsoft Excel file. The IWorkbook class contains a IWorksheetCollection that allows access to each worksheet in the Excel file. A worksheet is represented by the IWorksheet class. The IWorksheet class provides a ICells collection that represents all cells in the worksheet. The ICells collection provides several methods to manage rows or columns in a worksheet. Some of these are discussed below in more detail.
Setting the Height of a Row
It is possible to set the height of a single row by calling the ICells collection’s SetRowHeight method. The SetRowHeight method takes the following parameters as follows:
- Row index, the index of the row that you’re changing the height of.
- Row height, the row height to apply on the row.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleRowsAndColumns = dirPath->StringAppend(new String("sampleRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputRowsAndColumns = outPath->StringAppend(new String("outputRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Setting the height of the second row to 35 | |
worksheet->GetICells()->SetRowHeight(1, 35); | |
//Save the Excel file. | |
workbook->Save(outputRowsAndColumns); |
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 SetStandardHeight method of the ICells collection.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleRowsAndColumns = dirPath->StringAppend(new String("sampleRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputRowsAndColumns = outPath->StringAppend(new String("outputRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Setting the height of all rows in the worksheet to 25 | |
worksheet->GetICells()->SetStandardHeight(25); | |
//Save the Excel file. | |
workbook->Save(outputRowsAndColumns); |
Working with Columns
Setting the Width of a Column
Set the width of a column by calling the ICells collection’s SetColumnWidth method. The SetColumnWidth method takes the following parameters:
- Column index, the index of the column that you’re changing the width of.
- Column width, the desired column width.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleRowsAndColumns = dirPath->StringAppend(new String("sampleRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputRowsAndColumns = outPath->StringAppend(new String("outputRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Setting the width of the second column to 56.5 | |
worksheet->GetICells()->SetColumnWidth(1, 56.5); | |
//Save the Excel file. | |
workbook->Save(outputRowsAndColumns); |
Setting the Width of All Columns in a Worksheet
To set the same column width for all columns in the worksheet, use the ICells collection’s SetStandardWidth method.
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleRowsAndColumns = dirPath->StringAppend(new String("sampleRowsAndColumns.xlsx")); | |
//Path of output excel file | |
StringPtr outputRowsAndColumns = outPath->StringAppend(new String("outputRowsAndColumns.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Setting the width of all columns in the worksheet to 20.5 | |
worksheet->GetICells()->SetStandardWidth(20.5); | |
//Save the Excel file. | |
workbook->Save(outputRowsAndColumns); |