2016-06-09 80 views
0

我试图将A1表示法转换为int类型行和列,0起始索引。我得到了一个用C语言编写的代码,并试图制作一个C#代码。列是好的,但我在行中遇到问题。c#将A1表示法转换为R1C1算法

异常详细信息:System.FormatException:输入字符串的不正确的格式

row_col.Add( “行”,int.Parse(cellAddr + II));

private Dictionary<string, int> ParseA1Notation(string cellAddr) 
{ 
    Dictionary<string, int> row_col = new Dictionary<string, int>(); 
    int ii = 0, jj, colVal = 0; 
    while (cellAddr[ii++] >= 'A') { }; 
    ii--; 

    for (jj = 0; jj < ii; jj++) colVal = 26 * colVal + cellAddr[jj] - 'A' + 1; 
    row_col.Add("col", colVal); 
    row_col.Add("row", int.Parse(cellAddr + ii)); 
    return row_col; 
} 

继承人原来的C代码..

void rc(char * cellAddr, int *row, int *col) { 
    int ii=0, jj, colVal=0; 
    while(cellAddr[ii++] >= 'A') {}; 
    ii--; 
    // ii now points to first character of row address 
    for(jj=0;jj<ii;jj++) colVal = 26*colVal + toupper(cellAddr[jj]) -'A' + 1; 
    *col = colVal; 
    *row = atoi(cellAddr+ii); 
} 
+0

什么是你的'cellAddr'输入什么样子的? –

+0

字符串.. Excel像“A1”,“B2”,“C3”..即时尝试将其转换为(0,1),(2,3)..类似的东西.. –

+1

看看'row_col.Add (“行”,Int32.Parse(cellAddr.Substring(ii)))'作品 –

回答

1

请更改线路row_col.Add("row", int.Parse(cellAddr + ii))row_col.Add("row", Int32.Parse(cellAddr.Substring(ii)))