Get the Column Index of a Cell in Excel using OpenXML C#

前端 未结 8 1826
北恋
北恋 2020-12-03 17:32

I\'ve been looking around for a while now and cannot seem to find out how to do this. I\'ve got an excel sheet, which I\'m reading using OpenXML. Now the normal thing would

8条回答
  •  北海茫月
    2020-12-03 18:10

        public static void CellReferenceToIndex(string reference, out int row_index, out int col_index)
        {
            row_index = 0;
            col_index = 0;
    
            foreach(char c in reference)
            {
                if (c >= '0' && c <= '9')
                {
                    row_index = row_index * 10 + (c - '0');
                }
                if (c >= 'A' && c <= 'Z')
                {
                    col_index = col_index * ('Z' - 'A' + 1) + (c - 'A' + 1);
                }
            }
        }
    

提交回复
热议问题