C# - 是否可以将 Letter Coords(A1) 与 workSheet.Cells 一起使用?

C# - is it possible to use Letter Coords(A1) with workSheet.Cells?

我最近使用 powershell 通过 Excel 来自动化一些事情,我可以简单地使用 A1、A2 等。对于 C#,似乎需要使用 [1,1],(坐标样式),否则你会得到类型不匹配。 这是我正在使用的代码:

                    //Generating User and Password
                int startCoordI = Int32.Parse(startCoord);
                int endCoordI = Int32.Parse(endCoord);
                int userCoordI = Int32.Parse(userCoord);
                int passwordCoordI = Int32.Parse(passwordCoord);
                int value = startCoordI;
                string Username = Convert.ToString(workSheet.Cells[userCoord, startCoordI].Value);
                MessageBox.Show(Username);
                string Password = Convert.ToString(workSheet.Cells[passwordCoord, startCoordI].Value);
                MessageBox.Show(Password);

                try
                {
                    for (I = startCoordI; I <= endCoordI; I++)
                    {
                        System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("net", "user " + Username + " " + Password + " /add /passwordchg:no");
                        System.Diagnostics.Process proc = new System.Diagnostics.Process { StartInfo = proccessStartInfo };
                        proc.StartInfo.RedirectStandardOutput = true;
                        proc.StartInfo.UseShellExecute = false;
                        proccessStartInfo.CreateNoWindow = true;
                        proc.Start();

                        //new user
                        value++;
                        Username = Convert.ToString(workSheet.Cells[userCoord, value].Value);
                        Password = Convert.ToString(workSheet.Cells[passwordCoord, value].Value);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

这本质上不是问题,但如果能够使用 A1 样式坐标就好了。谢谢!

不是天生的,但是创建一个扩展方法来为你做这件事真的很容易(我没有安装 Excel,所以如果有任何类型错误,请更正它;)):

public static class ExcelExtensions
{
    public static Range Named(this Range Cells, string CellName)
    {

        char cellLetter = CellName.Substring(0, 1).ToUpper()[0];
        int xCoordinate = (cellLetter - 'A') + 1;
        int yCoordinate = int.Parse(CellName.Substring(1));
        return Cells[yCoordinate, xCoordinate];
    }
}

现在您可以:

workSheet.Cells.Named("B3").Value .....

在我看来,如果您尝试使用 BA1 或 AAA1 之类的东西进行操作,将会出现错误。

这个应该是对的:

public static class ExcelExtensions
    {
        public static exc.Range Named(this exc.Range Cells, string CellName)
        {
            //Get Letter
            char[] charArray = CellName.ToCharArray();
            string strLetter = string.Empty;
            foreach (char letter in charArray) 
            {

                if (Char.IsLetter(letter)) strLetter += letter.ToString();

            }

            //Convert Letter to Number

            double value = 0;

            if (strLetter.Length > 1)
            {

                foreach (char letter in strLetter) 
                {
                    if (value == 0)
                    {
                        value = (letter - 'A' + 1) * Math.Pow(26, (strLetter.Length -1));
                    }
                    else
                    {
                        value += (letter - 'A' + 1);
                    }

                }
            }

            else
            {
                char[] letterarray = strLetter.ToCharArray();
                value = (letterarray[0] - 'A') + 1;

            }

            // ReadOut Number

            string strNumber = string.Empty;
            foreach (char numChar in CellName.ToCharArray()) 
            {
                if (Char.IsNumber(numChar)) strNumber += numChar.ToString();

            }            

            return Cells[strNumber, value];

        }
    }

我知道,这是一个混乱的代码,但它有效:)

这对我来说似乎是最简洁的方法。处理了 AD14 列行问题。

public static Range Named(this Range Cells, string CellName)
    {

        int xCoordinate = Cells.Range[CellName].Column;
        int yCoordinate = Cells.Range[CellName].Row;
        return Cells[yCoordinate, xCoordinate];
    }