New to Telerik Document ProcessingStart a free 30-day trial

Get Cell Content Size

Updated on Jun 16, 2026

With the text measuring functionality of SpreadStreamProcessing you can get the size of specific content and use it to apply height and width to the rows and columns, respectively.

Why Is It Needed

SpreadStreamProcessing is designed to write the content directly inside the stream while you generate the document. While this brings great benefits in terms of performance and memory usage, it also introduces a limitation for automatic content sizing. To automatically fit the cell content into a column, the library must measure each cell content inside that column and change its width if needed. However, at the time the cell content is written into the stream, the column is already defined and cannot be further modified. To overcome that limitation, SpreadStreamProcessing exposes the CellContentSizeHelper class.

Using CellContentSizeHelper

CellContentSizeHelper is a static class and exposes two overloads of the GetCellContentSize method. These overloads allow you to pass the formatting applied to the cell as a SpreadCellFormat or as separate values.

To use this class, you must add a reference to Telerik.Windows.Documents.Spreadsheet for .NET Framework projects or Telerik.Documents.Spreadsheet for .NET Standard projects.

To achieve accurate results in .NET Standard, use SpreadFixedTextMeasurer.

Measuring Cell Content with SpreadCellFormat

One of the overloads of GetCellContentSize enables you to get the size needed for a specific cell value with an applied SpreadCellFormat. The following table describes the parameters of that method:

ParameterTypeDescription
valuestringThe cell value. If a formula is passed, the method throws an ArgumentException.
spreadCellFormatSpreadCellFormatThe formatting of the cell.
cellWidthdoubleOptional. The width of the cell in pixels. This value is only respected if the value of the WrapText property of the spreadCellFormat is true. The default column width is 65 pixels.

Measuring Cell Content with Separate Formatting Values

GetCellContentSize exposes an overload that allows you to pass separate values for the formatting properties of a cell that can affect the size of that cell content. The following table describes the parameters that overload accepts:

ParameterTypeDescription
valuestringThe cell value. If a formula is passed, the method throws an ArgumentException.
cellValueFormatstringThe number format of the cell.
fontFamilystringThe font family name.
fontSizedoubleThe size of the font.
isBoldboolA value indicating whether the text is bold.
isItalicboolA value indicating whether the text is italic.
horizontalAlignmentSpreadHorizontalAlignmentThe horizontal alignment.
isWrappedboolA value indicating whether the text is wrapped.
indentintThe cell indentation.
cellWidthdoubleOptional. The width of the cell in pixels. This value is only respected if the value of the WrapText property of the spreadCellFormat is true. The default column width is 65 pixels.

Example

The following example shows how to create a spreadsheet document, measure the content of the cells, and apply width to the columns so that these columns auto fit their content.

Example 1: Create Spreadsheet with Auto Fit Columns Width

C#
public static void ExportSpreadsheet(SpreadDocumentFormat documentFormat, string fileName)
{
    SpreadBorder border = new SpreadBorder(SpreadBorderStyle.Thin, new SpreadThemableColor(new SpreadColor(125, 125, 0)));
    SpreadCellFormat format = new SpreadCellFormat()
    {
        NumberFormat = "@",
        IsBold = true,
        IsItalic = true,
        LeftBorder = border,
        RightBorder = border,
        TopBorder = border,
        BottomBorder = border,
        WrapText = false
    };

    string[] vegetablesToExport = new string[] { "carrot", "broccoli", "cucumber", "eggplant", "lettuce", "cabbage", "tomato" };

    double vegetablesColumnWidth;
    CalculateColumnWidth(format, vegetablesToExport, out vegetablesColumnWidth);

    using (FileStream stream = File.Open(fileName, FileMode.OpenOrCreate))
    {
        using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(documentFormat, stream))
        {
            using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Vegetables List"))
            {
                using (IColumnExporter column = worksheet.CreateColumnExporter())
                {
                    column.SetWidthInPixels(vegetablesColumnWidth);
                }

                for (int rowIndex = 1; rowIndex < vegetablesToExport.Length + 1; rowIndex++)
                {
                    using (IRowExporter row = worksheet.CreateRowExporter())
                    {
                        string vegetable = vegetablesToExport[rowIndex - 1];

                        using (ICellExporter cell = row.CreateCellExporter())
                        {
                            cell.SetValue(vegetable);
                            cell.SetFormat(format);
                        }
                    }
                }
            }
        }
    }
}

private static void CalculateColumnWidth(SpreadCellFormat format, string[] values, out double columnWidth)
{
    columnWidth = 0;

    foreach (var value in values)
    {
        double contentWidth = CellContentSizeHelper.GetCellContentSize(value, format).Width;
        if (contentWidth > columnWidth)
        {
            columnWidth = contentWidth;
        }
    }
}

See Also