XlsFile.ColFromIndex Method
Overloads
XlsFile.ColFromIndex(Int32, Int32)
This is the column (1 based) for a given ColIndex. You can use this together with ExcelFile.ColCountInRow(Int32) and ExcelFile.ColToIndex(Int32, Int32) to iterate faster on a block. Or you can call ExcelFile.LoopOverUsedRange for a method that does the looping for you using those methods.
Syntax
Namespace: FlexCel.XlsAdapter
public override Int32 ColFromIndex(Int32 row, Int32 colIndex)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
row | Int32 | Row (1 based) | |
colIndex | Int32 | The index on the column list for the row. (1 based) |
Returns
The column (1 based) corresponding to the index when the index is inside the bounds. If the index is <= 0 or the index is > ExcelFile.ColCountInRow(Int32), it will return 0.
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));
}
}
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 ExcelFile.LoopOverUsedRange, so you might want to directly call ExcelFile.LoopOverUsedRange instead of pasting this example in your code.
See also
- XlsFile
- ExcelFile.ColCountInRow(Int32)
- ExcelFile.ColToIndex(Int32, Int32)
- ExcelFile.LoopOverUsedRange
XlsFile.ColFromIndex(Int32, Int32, Int32)
This is the column (1 based) for a given ColIndex and sheet. You can use this together with ExcelFile.ColCountInRow(Int32) and ExcelFile.ColToIndex(Int32, Int32) to iterate faster on a block. Or you can call ExcelFile.LoopOverUsedRange for a method that does the looping for you using those methods.
Syntax
Namespace: FlexCel.XlsAdapter
public override Int32 ColFromIndex(Int32 sheet, Int32 row, Int32 colIndex)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
sheet | Int32 | Sheet where we are working. It might be different from ActiveSheet. | |
row | Int32 | Row (1 based) | |
colIndex | Int32 | The index on the column list for the row. (1 based) |
Returns
The column (1 based) corresponding to the index when the index is inside the bounds. If the index is <= 0 or the index is > ExcelFile.ColCountInRow(Int32), it will return 0.
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));
}
}
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 ExcelFile.LoopOverUsedRange, so you might want to directly call ExcelFile.LoopOverUsedRange instead of pasting this example in your code.