Keep reference to particular cell?

Is there a way, how to keep a reference to the particular cell, even when rows are added and /or removed?

For example, I have a code like this:

    var file = new ExcelFile();
    var sheet = file.Worksheets.Add("Sheet1");

    sheet.Cells[2, 2].Value = "2,2";
    sheet.Cells[3, 2].Value = "3,2";

    var cell = sheet.Cells[2, 2];
    Console.WriteLine(cell.Value); 

    sheet.Rows.Remove(1);
    Console.WriteLine(cell.Value);

and I was kind of expecting to see twice “2,2” on the console, but I get “2,2” and “3,2” instead.

Is there another way, how to keep a reference to the cell?

Thanks a lot for the help

Hi Otakar,

No, I’m afraid there is no way.

You see, ExcelCell objects are basically proxies that just contain the information about their location (rows and columns position). Behind the scene, the cell data is stored in a memory-optimized structure.

When adding, inserting, or deleting rows and columns, only the cells’ data records are updated.
In order to provide support for your requirement (keep a reference to the cell), we would need to keep the reference of every ExcelCell, ExcelRow, and ExcelColumn object that was retrieved via API and then update their locations accordingly.

Besides the fact that this could cause some problems that would need to be handled (like with merged cells), the biggest disadvantage would be that it would decrease the performance by a lot.

I hope this makes sense.

Regards,
Mario