Hi guys,
I am currently working on upgrading Aspose.Cells from 23.6 to 25.10.
One of changes I noticed is that Cells[int] indexer is no longer available in 25.10.
Cells[int] used to return the Cell reference at the given column and the row where first value appears, which means the row index is not always 0.
Now I need to replace Cells[int] to Cells[int, int] which I have to provide explicit row index.
I converted old code like below
int firstRowIndex = CurrentWorkSheet.Cells.FirstCell?.Row ?? 0; // find first index of row with value given
CurrentWorkSheet.Cells[firstRowIndex, i].Value = ‘do something’
Is this right approach? or is there a better way?
Regards,
@sentientsoft
Your approach to replacing the Cells[int] indexer with Cells[int, int] is correct. The newer version of Aspose.Cells requires explicit row and column indices, and your code snippet effectively finds the first row index with a value and then accesses the cell using that index.
Here’s a refined version of your code for clarity:
int firstRowIndex = CurrentWorkSheet.Cells.FirstCell?.Row ?? 0; // Get the first row index with a value
CurrentWorkSheet.Cells[firstRowIndex, i].Value = "do something"; // Set the value in the specified column
This ensures that you are correctly accessing the cell based on the first row that contains a value. To handle cases where there might be no values in the worksheet, consider adding additional checks to avoid potential errors, such as verifying that firstRowIndex is within the valid range of rows.
If you have further questions or need more assistance, feel free to ask!
@sentientsoft
You can use Cells.GetEnumerator() method instead of Cells[int] method. Please use Cells.GetEnumerator() method to iterate all cells in this worksheet.
The sample code as follows:
Workbook wb = new Workbook(filePath + "sample.xlsx");
Cells cells = wb.Worksheets[0].Cells;
IEnumerator iter = cells.GetEnumerator();
while (iter.MoveNext())
{
Cell currCell = (Cell)iter.Current;
Console.WriteLine(currCell.Name);
}
Hope helps a bit.