Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Resize Rows and Columns

    • 7 minutes to read

    Use the Row.Height property to specify a row’s height and the Column.Width property to specify a column’s width.

    Row height
    The Row.Height property specifies the row height in document units. One document unit equals 1/300 of an inch (approximately 0.24 pt). The Row.Height property accepts values from 0 to 1706.25 (409.5 pt).
    Column width
    The Column.Width property specifies the column width in characters. Column width is not a physical measurement. It depends on the default font metrics specified by the workbook’s Normal style. The Column.Width property cannot be negative.

    The following code snippet sets the height of row 5 to 30 document units and the width of column 3 to 100 characters:

    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Rows[5].Height = 30;
    worksheet.Columns[3].Width = 100;
    

    The following code snippet sets the height of row 5 to the height of row 6 and the width of column 3 to the width of column 4:

    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Rows[5].Height = worksheet.Rows[6].Height;
    worksheet.Columns[3].Width = worksheet.Columns[4].Width;
    

    Note

    Set the Row.Height or Column.Width property to null to reset row height or column width to its default value.

    Resize Multiple Rows and Columns

    Call the RowRange.SetHeight(height) and ColumnRange.SetWidth(width) methods to specify the same height or width for all rows or columns in a range. These methods accept the same values as Row.Height and Column.Width properties.

    The following code snippet sets the height of rows 2:10 to 100 document units and the width of columns B:D to 20 characters:

    var worksheet = workbook.Worksheets[0];
    
    worksheet.Rows.GetRange("2:10").SetHeight(100);
    worksheet.Columns.GetRange("B:D").SetWidth(20);
    

    Fit Rows and Columns to Cell Content

    Call the following methods to fit row heights and column widths to cell content:

    Method Description
    Row.AutoFit() Fits the row height to cell content.
    Column.AutoFit() Fits the column width to cell content.
    CellRange.AutoFitRows() Fits the height of rows in the range to cell content.
    CellRange.AutoFitColumns() Fits column widths to the content of cells in the range.
    RowRange.AutoFit() Fits the height of rows in the range to cell content.
    ColumnRange.AutoFit() Fits the width of columns in the range to cell content.

    The following code snippet fits the width of column A and the height of row 1 to cell content and displays the effective column width and row height in the console:

    using System;
    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Chocolate Biscuits Mix";
    worksheet.Cells["B1"].Value = "Dry mix for chocolate biscuits. Add butter and milk, and bake for 12 minutes.";
    worksheet.Cells["B1"].Format.Alignment = new AlignmentFormat(
        SpreadsheetHorizontalAlignment.Left,
        SpreadsheetVerticalAlignment.Top).WithWrapText(true);
    worksheet.Columns[1].Width = 30;
    // Row 1 has a fixed height that does not fit the wrapped text.
    worksheet.Rows[0].Height = 62.5;
    
    worksheet.Columns[0].AutoFit();
    worksheet.Rows[0].AutoFit();
    
    Console.WriteLine($"Column A width: {worksheet.Columns[0].GetEffectiveWidth()}");
    Console.WriteLine($"Row 1 height: {worksheet.Rows[0].GetEffectiveHeight()}");
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    The CellRange.AutoFitRows method fits entire rows, and cells outside the range also affect the row height. The CellRange.AutoFitColumns method fits columns to cells in the range.

    The following code snippet fits the width of columns A and B to cells A2:B4 (the title in cell A1 does not affect the width) and fits the height of rows 2 to 4 to cell content:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Sales by Region for the First Quarter";
    worksheet.Cells["A2"].Value = "Region";
    worksheet.Cells["B2"].Value = "Revenue";
    worksheet.Cells["A3"].Value = "North America";
    worksheet.Cells["B3"].Value = 125000;
    worksheet.Cells["A4"].Value = "Europe";
    worksheet.Cells["B4"].Value = 98000;
    worksheet["A2:B2"].ApplyFont(new FontFormat("Calibri", 16));
    // Rows 2-4 have a fixed height that does not fit cell content.
    for (int rowIndex = 1; rowIndex <= 3; rowIndex++)
        worksheet.Rows[rowIndex].Height = 50;
    
    worksheet["A2:B4"].AutoFitColumns();
    worksheet["A2:A4"].AutoFitRows();
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Note

    Row.AutoFit, Column.AutoFit, CellRange.AutoFitRows, and CellRange.AutoFitColumns methods do not calculate formulas. Call the Workbook.Calculate() method before you fit rows or columns that contain new formulas or formulas that reference modified cells. Otherwise, row heights and column widths do not fit formula results.

    Get Effective Row Height and Column Width

    The Row.Height and Column.Width properties are null if a row or column does not have a specified size. Call Row.GetEffectiveHeight() and Column.GetEffectiveWidth() methods to get the row height and column width in effect. GetEffectiveHeight and GetEffectiveWidth methods return 0 for hidden rows and columns.

    The following code snippet gets the effective height of row 5 and the effective width of column 3:

    Worksheet worksheet = workbook.Worksheets[0];
    
    double rowHeight = worksheet.Rows[5].GetEffectiveHeight();
    double columnWidth = worksheet.Columns[3].GetEffectiveWidth();
    

    Set Default Row Height and Column Width

    Use Worksheet.DefaultRowHeight and Worksheet.DefaultColumnWidth properties to specify default sizes for rows and columns in a worksheet.

    DefaultRowHeight uses the same document units as Row.Height. DefaultColumnWidth uses the same character-based units as Column.Width.

    The following code snippet sets the default row height to 62.5 document units and the default column width to 20 characters:

    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.DefaultRowHeight = 62.5;
    worksheet.DefaultColumnWidth = 20;
    

    Note

    Set DefaultRowHeight or DefaultColumnWidth to null to remove the custom default value.

    Use GetEffectiveDefaultRowHeight() and GetEffectiveDefaultColumnWidth() methods to obtain default sizes that are actually in effect for the worksheet.

    The following code snippet obtains the effective default row height and column width:

    Worksheet worksheet = workbook.Worksheets[0];
    
    double defaultRowHeight = worksheet.GetEffectiveDefaultRowHeight();
    double defaultColumnWidth = worksheet.GetEffectiveDefaultColumnWidth();
    

    Note

    DefaultRowHeight and DefaultColumnWidth properties return null when the worksheet does not specify a custom default. Use GetEffectiveDefaultRowHeight() and GetEffectiveDefaultColumnWidth() methods to obtain the value that applies to rows or columns without their own size.

    Shared Worksheets

    A worksheet can belong to multiple workbooks. The effective default column width depends on the workbook’s Normal style font. A shared worksheet can have different effective default column widths in different workbooks.

    For a shared worksheet, Worksheet.GetEffectiveDefaultRowHeight, Worksheet.GetEffectiveDefaultColumnWidth, Row.GetEffectiveHeight, and Column.GetEffectiveWidth methods return values for the workbook in which you last called a Workbook.Calculate(sheet) or Workbook.CalculateFull method that includes the worksheet. To get the effective sizes for a specific workbook, call the Calculate method of this workbook first:

    workbook.Calculate(worksheet);
    double defaultColumnWidth = worksheet.GetEffectiveDefaultColumnWidth();
    double defaultRowHeight = worksheet.GetEffectiveDefaultRowHeight();
    
    See Also