Excel互操作问题-C#
我对Excel互操作对象有问题。我似乎也不能指出问题所在。(我的代码在底部)。基本上,有时它能正确读取单元格,但有时不能。两次通过后,它完全停止阅读。我想我有一道数学题,但我找不到。我会让别人读我的代码,但我附近没有人。我会回答一些问题。谢谢你的帮助,它为什么不起作用有点令人沮丧Excel互操作问题-C#,c#,excel,visual-studio,interop,C#,Excel,Visual Studio,Interop,我对Excel互操作对象有问题。我似乎也不能指出问题所在。(我的代码在底部)。基本上,有时它能正确读取单元格,但有时不能。两次通过后,它完全停止阅读。我想我有一道数学题,但我找不到。我会让别人读我的代码,但我附近没有人。我会回答一些问题。谢谢你的帮助,它为什么不起作用有点令人沮丧 if(requiredEnd == true && requiredPass == true && requiredPath == true && requiredSta
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!");
}
它基本上允许您使用PowerShell主题坐标,即(A1、B3等)Gusman在评论中回答了我的问题,因此我将在这里进行解释。我的问题是,我交换了x和y坐标,而循环中没有进程。谢谢你,古斯曼 当它错的时候它读什么?@Gusman你好,又是Gusman!谢谢你上次的帮助。这是一个关于发生了什么的Imgur图库:这是因为Excel交换了X和Y坐标,对不起,是我的错,更改返回单元格[xCoordinate,yCoordinate];返回单元格[yCoordinate,xCoordinate];它会的work@Gusman很抱歉再次打扰你,我真的很感谢你的帮助。我剩下的唯一问题是,它只创建第一个用户。我为我的胡闹道歉,我是C#和Excel Interop的新手。我只是在学习一般的编码知识。我认为问题在于我从For循环外部初始化net.exe,但是如果我不在循环外部创建它,我以后就不能在代码中引用它。你给了自己响应,你只执行了一次创建用户的过程,必须在循环中完成。不要在循环结束时等待(这就是为什么您需要循环外的进程),而是在lop内等待它完成,这样当循环结束时,所有用户都将被创建
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];
}
}