ExcelFile.ColToIndex Method
Overloads
ExcelFile.ColToIndex(Int32, Int32)
This is the inverse of ColFromIndex(Int32, Int32). It will return the index on the internal column array from the row for an existing column. If the column doesn't exist, it will return the index of the "LAST existing column less than col", plus 1. You can use this together with ColCountInRow(Int32) and ColFromIndex(Int32, Int32) to iterate faster on a block. Or you can call LoopOverUsedRange for a method that does the looping for you using those methods.
Syntax
Namespace: FlexCel.Core
public abstract Int32 ColToIndex(Int32 row, Int32 col)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
row | Int32 | Row (1 based) | |
col | Int32 | Column (1 based) |
Returns
The index on the column list for the row. (1 based)
Examples
To loop on all the existing cells range you can use:
// Loop at most until the last used row in the sheet.
// If LastRow is for example 1.000.000, but there are only
// 3 used rows, it makes no sense to loop over all the empty rows after row 3.
int LastUsedRow = Math.Min(LastRow, xls.RowCount);
for (int row = FirstRow; row <= LastUsedRow; row++)
{
int LastCIndex = xls.ColToIndex(row, LastColumn);
int LastColFromIndex = xls.ColFromIndex(row, LastCIndex);
if (LastColFromIndex > LastColumn || LastColFromIndex == 0) // LastColumn does not exist.
{
LastCIndex--;
}
if (LastCIndex == 0) continue; // This row is empty. Move to the next row.
int XF = -1;
for (int cIndex = xls.ColToIndex(row, FirstColumn); cIndex <= LastCIndex; cIndex++)
{
DoSomething(row, xls.ColFromIndex(row, cIndex), xls.GetCellValueIndexed(row, cIndex, ref XF));
}
}
Note that this example is the implementation of LoopOverUsedRange, so you might want to directly call LoopOverUsedRange instead of pasting this example in your code.
See also
ExcelFile.ColToIndex(Int32, Int32, Int32)
This is the inverse of ColFromIndex(Int32, Int32). It will return the index on the internal column array from the row for an existing column. If the column doesn't exist, it will return the index of the "LAST existing column less than col", plus 1. You can use this together with ColCountInRow(Int32) and ColFromIndex(Int32, Int32) to iterate faster on a block. Or you can call LoopOverUsedRange for a method that does the looping for you using those methods.
Syntax
Namespace: FlexCel.Core
public abstract Int32 ColToIndex(Int32 sheet, Int32 row, Int32 col)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
sheet | Int32 | Sheet where we are working. It might be different from ActiveSheet. | |
row | Int32 | Row (1 based) | |
col | Int32 | Column (1 based) |
Returns
The index on the column list for the row. (1 based)
Examples
To loop on all the existing cells on a range you can use:
// Loop at most until the last used row in the sheet.
// If LastRow is for example 1.000.000, but there are only
// 3 used rows, it makes no sense to loop over all the empty rows after row 3.
int LastUsedRow = Math.Min(LastRow, xls.RowCount);
for (int row = FirstRow; row <= LastUsedRow; row++)
{
int LastCIndex = xls.ColToIndex(row, LastColumn);
int LastColFromIndex = xls.ColFromIndex(row, LastCIndex);
if (LastColFromIndex > LastColumn || LastColFromIndex == 0) // LastColumn does not exist.
{
LastCIndex--;
}
if (LastCIndex == 0) continue; // This row is empty. Move to the next row.
int XF = -1;
for (int cIndex = xls.ColToIndex(row, FirstColumn); cIndex <= LastCIndex; cIndex++)
{
DoSomething(row, xls.ColFromIndex(row, cIndex), xls.GetCellValueIndexed(row, cIndex, ref XF));
}
}
Note that this example is the implementation of LoopOverUsedRange, so you might want to directly call LoopOverUsedRange instead of pasting this example in your code.