[英]Excel Interop Problems - C#
我對Excel Interop對象有問題。 我似乎也無法查明問題所在。 (我的代碼在底部)。 基本上,有時它會正確讀取單元格,但有時卻不能。 經過兩遍后,它將完全停止讀取。 我想我有一個數學問題,但找不到。 我會讓別人閱讀我的代碼,但附近沒有人。 我待會兒回答問題。 感謝您的幫助,它為什么不起作用有點令人沮喪。
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!");
}
那里有很多“不需要”的部分,但是我認為把整個事情都付諸實踐會更有幫助。 (大多數文本框只是調試內容,順便說一句)
編輯:我忘記了。命名不是正常代碼的一部分。 它是某人為我創建的功能:
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等)
古斯曼(Gusman)在評論中回答了我的問題,因此我將在此處進行解釋。 我的問題是我交換了x和y坐標,並且循環中沒有該過程。 謝謝古斯曼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.