c#中将Excel列转换为整数并再转换回来的简单例程是什么?

本文关键字:转换 简单 回来 例程 是什么 Excel 中将 整数 | 更新日期: 2023-09-27 18:15:44

我试图通过编程迭代在微软Excel外接程序的Excel电子表格的列。从字母转换为数字应该很简单(并再次返回以在列标题上执行算术),然后在移动到"Z"(或"A",如果反向移动)之后迭代到下一个alpha块,但是我在让它工作时遇到了麻烦。谁能建议一套简单的算法来使用CSharp?

c#中将Excel列转换为整数并再转换回来的简单例程是什么?

下面是一组使用基本字符串操作、除法和模数的简单例程:

    private int DoGetIntFromColPart(string strColPart)
    {
        int nReturn = 0;
        try
        {
            strColPart = strColPart.ToLower();
            if (strColPart == "a")
                nReturn = 1;
            else if (strColPart == "b")
                nReturn = 2;
            else if (strColPart == "c")
                nReturn = 3;
            else if (strColPart == "d")
                nReturn = 4;
            else if (strColPart == "e")
                nReturn = 5;
            else if (strColPart == "f")
                nReturn = 6;
            else if (strColPart == "g")
                nReturn = 7;
            else if (strColPart == "h")
                nReturn = 8;
            else if (strColPart == "i")
                nReturn = 9;
            else if (strColPart == "j")
                nReturn = 10;
            else if (strColPart == "k")
                nReturn = 11;
            else if (strColPart == "l")
                nReturn = 12;
            else if (strColPart == "m")
                nReturn = 13;
            else if (strColPart == "n")
                nReturn = 14;
            else if (strColPart == "o")
                nReturn = 15;
            else if (strColPart == "p")
                nReturn = 16;
            else if (strColPart == "q")
                nReturn = 17;
            else if (strColPart == "r")
                nReturn = 18;
            else if (strColPart == "s")
                nReturn = 19;
            else if (strColPart == "t")
                nReturn = 20;
            else if (strColPart == "u")
                nReturn = 21;
            else if (strColPart == "v")
                nReturn = 22;
            else if (strColPart == "w")
                nReturn = 23;
            else if (strColPart == "x")
                nReturn = 24;
            else if (strColPart == "y")
                nReturn = 25;
            else if (strColPart == "z")
                nReturn = 26;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error [DoGetIntFromColPart]: " + ex);
        }
        return nReturn;
    }
    private int DoGetIntFromCol(string strCol)
    {
        int nCol = 0;
        try
        {
            int nPlace = 0;
            strCol = strCol.ToLower();
            for (int nCount = strCol.Length - 1; nCount >= 0; nCount--)
            {
                nCol += DoGetIntFromColPart(strCol.Substring(nCount, 1)) * (int)(Math.Pow(26, nPlace));
                nPlace++;
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error [DoGetIntFromCol]: " + ex);
        }
        return nCol;
    }
    private string DoGetColFromInt(int nCol)
    {
        string strCol = String.Empty;
        try
        {
            int nDiv = nCol;
            int nMod = 0;
            while (nDiv > 0)
            {
                nMod = (nDiv - 1) % 26;
                strCol = (char)(65 + nMod) + strCol;
                nDiv = (int)((nDiv - nMod) / 26);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error [DoGetColFromInt]: " + ex);
        }
        return strCol.ToLower();
    }