How to auto adjust height of cells which have wrap text property applied

Posted by: aditya.chauhan on 17 March 2025, 1:06 am EST

  • Posted 17 March 2025, 1:06 am EST

    i am using spread.net in my application in which reports use an excel sheet as a template

    so what if i have applied wrap text property to a column then how do i achieve this functionality where the height of the cell auto adjusts according to the content but only and only to the cells where wrap text was applied in the excel sheet

    do i have to iterate over every cell to check for the wrap text property

    is there not a way to automatically auto adjust the height of cells which have wrap text property enabled

  • Posted 18 March 2025, 12:31 pm EST

    Hi Aditya,

    Thanks for reaching out to us.

    We are currently discussing this requirement with our developers. [Internal Tracking ID: SPNET-46771] We will update you on this as soon as we hear back from them.

    Thanks & regards,

    Aastha

  • Posted 20 March 2025, 5:42 am EST

    Hi Aditya,

    You can use the following line of code to automatically fit all the rows in the FpSpread control:

    fpSpread1.ActiveSheet.AsWorksheet().Rows.AutoFit();

    Kindly refer to the attached sample for full implementation. See WrapTextHeight.zip

    Thanks & regards,

    Aastha

  • Posted 20 March 2025, 5:44 am EST

    i dont want to auto adjust the height of every cell in the sheet i want only those cells height to be auto adjusted which have the property wrap text enabled

  • Posted 20 March 2025, 6:59 am EST

    Hi Aditya,

    For this use-case, you have to iterate over every cell to check for the wrap text property.

    For a small amount of data, the following code can be used to ignore the empty rows/columns:

    fpSpread1.SuspendLayout();
    int lastNonEmptyRowCount = sheet.GetLastNonEmptyColumn(FarPoint.Win.Spread.NonEmptyItemFlag.Data);
    int lastNonEmptyColumnCount = sheet.GetLastNonEmptyRow(FarPoint.Win.Spread.NonEmptyItemFlag.Data);
    for (int row = 0; row < lastNonEmptyRowCount; row++)
    {
        bool hasWrap = false;
        fpSpread1.ActiveSheet.AsWorksheet().Rows[row].AutoFit();
        //Iterate only through columns you care about, or entire row
        for (int col = 0; col < lastNonEmptyColumnCount; col++)
        {
            if (sheet.AsWorksheet().Cells[row, col].WrapText)
            {
                hasWrap = true;
                break;
            }
        }
        if (hasWrap)
        {
            sheet.Rows[row].Height = sheet.Rows[row].GetPreferredHeight();
        }
    }
    fpSpread1.ResumeLayout();

    Kindly refer WrapTextHeight_Mod.zip to see the updated sample.

    Thanks & regards,

    Aastha

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels