In my code I have comething like:
for (int i = 0; i < worksheet.Cells.Count; i++) {
if ( worksheet.Cells[worksheet.Cells[i].Row, 0].StringValue.Trim() == String.Empty &&
worksheet.Cells[i].Row >= XLS_FIRSTROW
) {
Cell currentCell = worksheet.Cells[i];
if (currentCell.StringValue.Trim().Length > 0)
…
and I founded something very strange for me. I stopped loop when I reach second column in the next row, and in debug window I got following:
? worksheet.Cells.Count
1013563
? worksheet.Cells[worksheet.Cells[i].Row, 0].StringValue.Trim()
""
? worksheet.Cells.Count
1013564
Looks like, when I touch previous cell by worksheet.Cells[worksheet.Cells[i].Row, 0],
in cells collection I got one element more.
Do you have any explanation?
On the end of the previous loop, in my example, “worksheet.Cells.Count” return me 1078315, this mean around 4500 cells more then before the “foor” loop???
Regards
Hi,
Thanks for considering Aspose.
Well, I think It might possible that you used or filled those extra 4500 cells with some data or change their settings (height width), formattings and then erase the data from the cells or clear the formattings. Once, you make use of any cell, it will be automatically added to add Cells collection list and increase the Cells.Count. The cells with default settings or formattings are not included into Cells collection.
Thank you.
Thank for you quick answer, but in very simple example I received different values from the same cell:
for (int i = 0; i < worksheet.Cells.Count; i++) {
…one value/cell in the cell with index i
Debug.Write(worksheet.Cells[i].StringValue);
//critical line
string firstCell = worksheet.Cells[worksheet.Cells[i].Row, 0].StringValue;
…another value/cell in the cell with index i
Debug.Write(worksheet.Cells[i].StringValue);
}
Critical line rearange collection and all my cells are shifted, and my results are wrong.
I didn’t change anything in cells, I just reading values.
Hi,
I think you or someone else may have used or disturbed settings or inserted deleted data of those cells in the template file in MS Excel.
Could you try conduct a simple test and create a new file into MS Excel and then use Aspose.Cells to check the Cells collection.
If you still find some problem, do post your template file and sample code with details here, We will check to figure it our soon.
And Which version of Aspose.Cells you are using? Kindly try the attached version.
Thank you.
Cells.Count is volatile because we use a lazy allocation method to create cells collection to save memory.
For example,
You create an Excel file and put a value in B1. When you use Aspose.Cells to open this file, Cells.Count will return 1 for this worksheet.
However, if you use Cells[0, 0].StringValue to read value from Cell A1, a new Cell object will be created. Then Cells.Count will return 2.