Excel互操作问题- c#

本文关键字:问题 互操作 Excel | 更新日期: 2023-09-27 18:10:23

我有一个问题与Excel互操作对象。我似乎也找不到问题所在。(我的代码在底部)。基本上,有时它能正确读取细胞,但有时不能。两遍之后,它完全停止读取。我想我有数学问题,但是我找不到它。我想让别人来读我的代码,但我附近没有人。我还会继续回答你们的问题。谢谢你的帮助,为什么它不工作,这有点令人沮丧。

if(requiredEnd == true && requiredPass == true && requiredPath == true && requiredStart == true && requiredUser == true && requiredSheet == true)
        {
            errorCheck = false;
            try
            {

                //starting Excel
                Excel.Application excelApp = new Excel.Application();
                excelApp.Visible = false;
                Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
                Excel.Sheets sheet = workBook.Worksheets;
                Excel.Worksheet workSheet = (Excel.Worksheet)sheet.get_Item(sheetName);

                //Generating User and Password
                int startCoordI = Int32.Parse(startCoord);
                int endCoordI = Int32.Parse(endCoord);
                int value = startCoordI;
                string combinedUser = userCoord + startCoord;
                string combinedPassword = passwordCoord + startCoord;
                string Username = Convert.ToString(workSheet.Cells.Named(combinedUser).Value);
                MessageBox.Show(Username);
                string Password = Convert.ToString(workSheet.Cells.Named(combinedPassword).Value);
                MessageBox.Show(Password);
                try
               {
                    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;
                    for (I = startCoordI; I <= endCoordI; I++)
                    {
                        proc.Start();
                        //new user
                        value++;
                        combinedUser = userCoord + value;
                        combinedPassword = passwordCoord + value;
                        Username = Convert.ToString(workSheet.Cells.Named(combinedUser).Value);
                        MessageBox.Show(Username);
                        Password = Convert.ToString(workSheet.Cells.Named(combinedPassword).Value);
                        MessageBox.Show(Password);
                    }
                    //Clean up Excel
                    Marshal.ReleaseComObject(excelApp);
                    Marshal.ReleaseComObject(workBook);
                    Marshal.ReleaseComObject(sheet);
                    Marshal.ReleaseComObject(workSheet);

                    //Executing.Show
                    proc.WaitForExit();
                    //Executing.Close

                     if(proc.HasExited == true)
                     {
                         if(errorCheck == false)
                         {
                             MessageBox.Show("The Process Has Been Completed!");
                         }
                         else
                         {
                             MessageBox.Show("Operation Ended With Errors. Exiting Excel Reader.");
                         }
                         proc.Close();
                         this.Close();
                     }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
           catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            if(requiredEnd == false || requiredPass == false || requiredStart == false || requiredUser == false || requiredSheet == false)
            {
                MessageBox.Show("You Have Missing Required Fields!");
            }
            else
            {
                MessageBox.Show("That Is Not A Valid File!");
            }

有很多"不需要"的部分,但我认为给出整个东西会更有帮助。(大多数文本框只是调试的东西,BTW)

编辑:我忘了。named不是正常代码的一部分。这是某人为我设计的功能:

    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[xCoordinate, yCoordinate];
    }
}

它基本上允许你使用PowerShell主题坐标,即(A1, B3等)

Excel互操作问题- c#

Gusman已经在评论中回答了我的问题,所以我在这里解释一下。我的问题是我交换了x和y坐标,而且我没有在循环中进行进程。谢谢你,格斯曼!