I am reading an Excel document through a library DocumentFormat.OpenXml. Is there a good way to find out how many columns he has?
In the current code that I just discovered while investigating the error, do the following:
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 a link to the cell (for example, "CB99") and get everything up to 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 from A to M, so the function should return the string "M". But since there is an extraneous space character in cell C1522, which is considered the last cell, so the function tells the column max as "C".
My initial impulse was to simply replace this challenge Last()with some kind Max(columnNumber). However Cell, it does not seem to show the actual column number, only this composite row CellReference. I don’t think I want to do line splitting inside a predicate.
Is there a way to find the rightmost column of a worksheet without having to analyze CellReferenceeach individual cell?