How to: Specify Row Height or Column Width
- 5 minutes to read
Set Row Height
This example demonstrates how to control the row height in a worksheet.
Set Height for Individual Rows
Use the Row.Height or CellRange.RowHeight property to set the row height in the workbook’s measurement units.
Note
If the row height is set to 0, the row is hidden. You can also use the Row.Visible property to hide a row or display the hidden row again.
AutoFit Row Height
To automatically change the row height to fit the contents, use the row’s Row.AutoFit method. To quickly autofit several rows in a worksheet, use the RowCollection.AutoFit method.
Use the SpreadsheetCellOptions.AutoFitMergedCellRowHeight property to enable the AutoFit functionality for rows that have cells merged across.
Match the Height of Two Rows
To match one row height to another, make their Row.Height property values equal.
Set the Default Row Height
To set the default height for worksheet rows, use the Worksheet.DefaultRowHeight property. This property does not affect rows with an explicitly specified height.
Set the Height for All Rows in a Worksheet
To change the height of all rows in a worksheet, use the CellRange.RowHeight property of the Column object corresponding to any column in a worksheet. This sets the height of all rows to the same value, overriding all previously applied row height settings.
// Set the height of the 3rd row to 50 points
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.Rows[2].Height = 50;
// Set the height of the row that contains the "C5" cell to 2 inches.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
worksheet.Cells["C5"].RowHeight = 2;
// Set the height of the 7th row to the height of the 3rd row.
worksheet.Rows["7"].Height = worksheet.Rows["3"].Height;
// Set the default row height to 30 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.DefaultRowHeight = 30;
Set Column Width
This example demonstrates how to control the column width in a worksheet.
Set the Width for Individual Columns
To specify the column width in characters of the default font specified by the built-in Normal style, use the Column.WidthInCharacters or CellRange.ColumnWidthInCharacters property.
To specify the column width in pixels, use the column’s Column.WidthInPixels property.
To set the column width in other measurement units, set the Workbook.Unit
property to the required DocumentUnit enumeration member and specify the Column.Width or CellRange.ColumnWidth property.
Note
If the column width is set to 0, the column is hidden. You can also use the Column.Visible property to hide a column or display the hidden column again.
AutoFit Column Width
To automatically change the column width to fit the contents, use the columns’s Column.AutoFit method. To quickly autofit several columns in a worksheet, call the ColumnCollection.AutoFit method.
Match the Width of Two Columns
To match one column width to another, you can use the Column.Width property or the CellRange.CopyFrom method of the column object.
Set the Default Column Width
To set the default width for worksheet columns, use the Worksheet.DefaultColumnWidthInCharacters, Worksheet.DefaultColumnWidthInPixels or Worksheet.DefaultColumnWidth property. These properties do not affect columns with an explicitly specified width.
Set the Width for All Columns in a Worksheet
To change the width of all columns in a worksheet, use the CellRange.ColumnWidthInCharacters or CellRange.ColumnWidth property of the Row object corresponding to any row in a worksheet. This sets the width of all columns to the same value, overriding all previously applied column width settings.
// Set the "B" column width to 30 characters of the default font that is specified by the Normal style.
worksheet.Columns["B"].WidthInCharacters = 30;
// Set the "C" column width to 15 millimeters.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
worksheet.Columns["C"].Width = 15;
// Set the width of the column that contains the "E15" cell to 100 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.Cells["E15"].ColumnWidth = 100;
// Set the width of all columns that contain the "F4:H7" cell range (the "F", "G" and "H" columns) to 70 points.
worksheet.Range["F4:H7"].ColumnWidth = 70;
// Set the "J" column width to the "B" column width value.
worksheet.Columns["J"].Width = worksheet.Columns["B"].Width;
// Copy the "C" column width value and assign it to the "K" column width.
worksheet.Columns["K"].CopyFrom(worksheet.Columns["C"], PasteSpecial.ColumnWidths);
// Set the default column width to 40 pixels.
worksheet.DefaultColumnWidthInPixels = 40;
Note
If an application is hosted on Microsoft Azure, set the AzureCompatibility.Enable property to true on application startup to export autofit rows and columns to PDF.