I'm reading an Excel document via the DocumentFormat.OpenXml
library. Is there a good way to find out how many columns it has?
The current code, which I've just come across while investigating a bug, does this:
public string getMaxColumnName(SheetData aSheetData)
{
string lLastCellReference = aSheetData.Descendants<Cell>().Last().CellReference.InnerText;
char[] lRowNumberIndex = lLastCellReference.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
return lLastCellReference.Substring(0, lRowNumberIndex);
}
In English: find the last cell in the sheet, get its cell reference (like "CB99"), and retrieve everything before the first digit. The problem is that the last cell in the sheet is not necessarily in the rightmost column.
I have a test sheet that is a neat, rectangular table. It has 1000 rows filling columns A through M, so the function is supposed to return the string "M". But because there is an extraneous space character in cell C1522, that's counted as the last cell, so the function reports the max column as "C".
My initial impulse was to just replace that Last()
call with some kind of Max(columnNumber)
. However, Cell
apparently does not expose an actual column number, only this composite CellReference
string. I don't think I want to be doing string-splitting inside a predicate there.
Is there a way to find the sheet's rightmost column, without having to parse the CellReference
of every single cell?