Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Cell.CellReference optional? How do I get cell location otherwise?

According to the OpenXml spec CellReference is optional. But is there an alternative way to get a cell's location?

For example, I can get a row's cells by something like:

foreach (Row r in sheetData.Elements<Row>())
{
    foreach (Cell c in r.Elements<Cell>())
    {
        Console.WriteLine(c.CellReference);
    }
} 

But without CellReference, how do I know the cells' locations?

I have done some research and all solutions seem to rely on comparing a cell's CellReference against a given string.

like image 234
user1792714 Avatar asked Nov 01 '12 22:11

user1792714


1 Answers

The CellReference is optional because if it's missing, Excel uses the "first come first served" principle (that's not the official term by the way).

What happens when all your Cell classes have missing CellReference properties is that Excel will parse based on the order in which it encounters Row and Cell classes.

The 1st Row class (you will note that the RowIndex is also an optional property) that's in the SheetData class will be assumed to have row index 1. And the 1st Cell class in that row is assumed to have the CellReference "A1" (assuming the Cell has no CellReference assigned). And the 2nd Cell class in that row is assumed to have the CellReference "B1". And so on and so forth.

The next Row class (if the RowIndex property is also not assigned) will be assumed to be the 2nd row.

Basically, if all the RowIndex and CellReference properties are missing, all your Cell classes are bunched together at the top-left corner of your worksheet.

Hypothetically speaking, if you have 3 unassigned RowIndex-ed Row classes and the next Row class has a RowIndex of 8, that's a valid worksheet. You'll just have the first 3 rows of data in the first 3 rows in the worksheet (as expected), and the "4th" Row class (with the RowIndex of 8) as the 8th row in the worksheet.

like image 195
Vincent Tan Avatar answered Nov 15 '22 08:11

Vincent Tan