In ClosedXML, anyway, to get a column letter on behalf of the column header?

I have an excel table with column headers, and I don't want to hardcode the letter or index of the column, so I'm trying to figure out how I can make it dynamic. I am looking for something like this:

var ws = wb.Worksheet("SheetName"); var range = ws.RangeUsed(); var table = range.AsTable(); string colLetter = table.GetColumnLetter("ColHeader"); foreach (var row in table.Rows()) { if (i > 1) { string val = row.Cell(colLetter).Value.ToString(); } i++; } 

Does ClosedXML support something like the created GetColumnLetter () function above, so I don’t need to write the letters of a column with hard code?

+6
source share
1 answer

Of course, get the desired cell using the predicate in the CellsUsed collection in the header row, then return the column letter from the column.

 public string GetColumnName(IXLTable table, string columnHeader) { var cell = table.HeadersRow().CellsUsed(c => c.Value.ToString() == columnHeader).FirstOrDefault(); if (cell != null) { return cell.WorksheetColumn().ColumnLetter(); } return null; } 
+7
source

Source: https://habr.com/ru/post/984979/


All Articles