Search Results for

    Show / Hide Table of Contents

    ExcelFile.ColCountInRow Method

    Overloads

    • ExcelFile.ColCountInRow(Int32)
    • ExcelFile.ColCountInRow(Int32, Int32)

    ExcelFile.ColCountInRow(Int32)

    This method returns the NOT EMPTY cells on ONE ROW.

    Note that this is not the full column count, but only the count of those columns that have any data on them for the given row. So for example if in row 2 you have a value in A2 and another in E2, ColCountInRow(2) will return 2 (cells A2 and E2), not 5 (columns from A to E).

    You can use this together with ColFromIndex(Int32, Int32) and ColToIndex(Int32, Int32) to iterate faster on a block, skipping all empty cells.

    Syntax

    Namespace: FlexCel.Core

    public abstract Int32 ColCountInRow(Int32 row)

    Parameters

    <-> Parameter Type Description
    row Int32 Row index. (1-based)

    Returns

    The number of existing columns on one row.

    Examples

    Instead of writing:

        int RowCount = xls.RowCount;
        int ColCount = xls.ColCount;
        for (int row = 1; row <= RowCount; row++)
        {
            for (int col = 1; col <= ColCount; col++) //It would be faster to use ColCountInRow. See https://doc.tmssoftware.com/flexcel/net/guides/performance-guide.html#avoid-calling-colcount
            {
                DoSomething(row, col);
            }
        }
    

    You can use:

        int RowCount = xls.RowCount;
        for (int row = 1; row <= RowCount; row++)
        {
            int ColCountInRow = xls.ColCountInRow(row);
            for (int colIndex = 1; colIndex <= ColCountInRow; colIndex++)
            {
                DoSomething(row, xls.ColFromIndex(row, colIndex));
            }
        }
    

    See also

    • ExcelFile
    • ColFromIndex(Int32, Int32)
    • ColToIndex(Int32, Int32)
    • LoopOverUsedRange

    ExcelFile.ColCountInRow(Int32, Int32)

    This method returns the NOT EMPTY cells on ONE ROW, for a given sheet.

    Note that this is not the full column count, but only the count of those columns that have any data on them for the given row. So for example if in row 2 you have a value in A2 and another in E2, ColCountInRow(2) will return 2 (cells A2 and E2), not 5 (columns from A to E).

    You can use this together with ColFromIndex(Int32, Int32) and ColToIndex(Int32, Int32) to iterate faster on a block, skipping all empty cells. Or you can call LoopOverUsedRange for a method that does the looping for you using those methods.

    Syntax

    Namespace: FlexCel.Core

    public abstract Int32 ColCountInRow(Int32 sheet, Int32 row)

    Parameters

    <-> Parameter Type Description
    sheet Int32 Sheet where we are working. It might be different from ActiveSheet.
    row Int32 Row index. (1-based)

    Returns

    The number of existing columns on one row.

    Examples

    Instead of writing:

        int RowCount = xls.RowCount;
        int ColCount = xls.ColCount;
        for (int row = 1; row <= RowCount; row++)
        {
            for (int col = 1; col <= ColCount; col++) //It would be faster to use ColCountInRow. See https://doc.tmssoftware.com/flexcel/net/guides/performance-guide.html#avoid-calling-colcount
            {
                DoSomething(row, col);
            }
        }
    

    You can use:

        int RowCount = xls.RowCount;
        for (int row = 1; row <= RowCount; row++)
        {
            int ColCountInRow = xls.ColCountInRow(row);
            for (int colIndex = 1; colIndex <= ColCountInRow; colIndex++)
            {
                DoSomething(row, xls.ColFromIndex(row, colIndex));
            }
        }
    

    See also

    • ExcelFile
    • ColFromIndex(Int32, Int32)
    • ColToIndex(Int32, Int32)
    • LoopOverUsedRange
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com