reading Excel Open XML is ignoring blank cells

后端 未结 14 1871
忘了有多久
忘了有多久 2020-11-30 04:18

I am using the accepted solution here to convert an excel sheet into a datatable. This works fine if I have \"perfect\" data but if I have a blank cell in the middle of my

14条回答
  •  天命终不由人
    2020-11-30 05:05

    You can use this function to extract a cell from a row passing the header index:

    public static Cell GetCellFromRow(Row r ,int headerIdx) {
            string cellname = GetNthColumnName(headerIdx) + r.RowIndex.ToString();
            IEnumerable cells = r.Elements().Where(x=> x.CellReference == cellname);
            if (cells.Count() > 0)
            {
                return cells.First();
            }
            else {
                return null;
            }
    }
    public static string GetNthColumnName(int n)
        {
            string name = "";
            while (n > 0)
            {
                n--;
                name = (char)('A' + n % 26) + name;
                n /= 26;
            }
            return name;
        }
    

提交回复
热议问题