2014-02-24 105 views
-1

我運行下面的代碼,如果我一步直通代碼它顯示我的價值,但是當它到達變量未顯示價值

if (Directory.Exists(folderPath)) 

FNAME沒有在FOLDERPATH被顯示。它只是舉例說明。

"\\\\Delta\\" + fName + "_Monday\\" 

有人可以告訴我我應該更新以使其正常工作嗎?

public static void OpenExcelWorkbook() 
{ 
    fName = new string[4] { "Mike", "Joe", "Hickney", "Rich", }; 
    folderPaths = new string[7] 
    { 
     "\\\\Delta\\" + fName + "_Monday\\", 
     "\\\\Delta\\" + fName + "_Tuesday\\", 
     "\\\\Delta\\" + fName + "_Wednesday\\", 
     "\\\\Delta\\" + fName + "_Thursday\\", 
     "\\\\Delta\\" + fName + "_Friday\\", 
     "\\\\Delta\\" + fName + "_Saturday\\", 
     "\\\\Delta\\" + fName + "_Sunday\\", 
    }; 
    fileNames = new string[4] 
    { 
     fName + "_generaldaily_file.xlsx", 
     fName + "_employeeDaily_cumulative.xls", 
     fName + "_generaldaily_file.xlsx", 
     fName + "_employeeDaily_cumulative.xls", 
     }; 

    Excel.Workbook wb = null; 
    try 
    { 
     for (int q = fName.GetLowerBound(0); q <= fName.GetUpperBound(0); q++) 
     { 
      foreach (string fileName in fileNames) 
      { 
       foreach (string folderPath in folderPaths) 
       { 
        if (Directory.Exists(folderPath)) 
        { 
         foreach (string filePath in Directory.GetFiles(folderPath)) 
         { 
          string temp = Path.GetFileName(filePath).ToLower(); 
          if (temp == fileName.ToLower()) 
          { 
           oWB = (Excel._Workbook)(oXL.Workbooks.Open(folderPath + fileName)); 
           oWB = oXL.ActiveWorkbook; 
           oWB.RefreshAll(); 
           //Calling method to save workbook here 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    catch { } 
} 
+5

fname在哪裏申報?我在這裏看不到代碼。 – ThunderGr

+0

我會補充說,肯定給我一分鐘 – MasterOfStupidQuestions

+0

'它只顯示例如。 「\\\\ Delta \\」+ fName +「_Monday \\」' 你是什麼意思?任何字符串如何顯示? – Tarec

回答

2

一個建議值,使用Path.Combine而不是手動連接目錄名稱,並且最好使用@""作爲字符串文字,避免需要轉義反斜槓。例如

Path.Combine(@"\\Delta", fName + "_Monday"); 

你不應該需要預先生成的路徑名和文件名作爲數組的名單,但只是按需創建它們,你通過名稱的列表進行迭代。

string root = @"\\Delta"; 

String[] dayOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; 
String[] fileNames = { "generaldaily_file.xlsx", "employeeDaily_cumulative.xls", "generaldaily_file.xlsx", "employeeDaily_cumulative.xls" }; 
String[] fName = { "Mike", "Joe", "Hickney", "Rich" }; 

foreach (string name in fName) { 
    foreach (string day in dayOfWeek) 
     var folderPath = Path.Combine(root, name + "_" + day); 
     if (Directory.Exists(folderPath)) { 
      foreach (string filePath in Directory.GetFiles(folderPath) { 
       string temp = Path.GetFileName(filePath); 
       foreach (string fileName in fileNames) { 
        if (temp.toLower() == (name + "_" + fileName).toLower()) { 
         ... 
        } 
       } 
      } 
     } 
    } 
} 

另外,不要吞掉異常空catch塊,否則你會不知道,如果東西壞了什麼問題。至少應該打印或記錄堆棧跟蹤,以便確定問題出在哪裏。

+0

+1的時候就發現了這個需求。不過,我不認爲Path.Combine是必要的。 – ThunderGr

0

我重構代碼以他下面的,因爲我不知道你是如何分配給FNAME它會更好,如果傳遞的參數明確

public static void OpenExcelWorkbook(string fName) 
    { 
     public string[] GetFolderPaths() 
     { 
      return new string[7] = { 
      string.format("\\\\Delta\\{0}_Monday\\",fName), 
      string.format("\\\\Delta\\{0}_Tuesday\\",fName), 
      string.format("\\\\Delta\\{0}_Wednesday\\",fName), 
      string.format("\\\\Delta\\{0}_Thursday\\",fName), 
      string.format("\\\\Delta\\{0}_Friday\\",fName), 
      string.format("\\\\Delta\\{0}_Saturday\\",fName), 
      string.format("\\\\Delta\\{0}_Sunday\\",fName) 
      } 
     }; 
     // the same applies to the filenames 
     public string[] GetFileNames(){ 
      return new string[4] ={ 
      string.format("{0}_generaldaily_file.xlsx",fName), 
      string.format("{0}_employeeDaily_cumulative.xls",fName), 
      string.format("{0}_generaldaily_file.xlsx",fName), 
      string.format("{0}_employeeDaily_cumulative.xls",fName)} 

     } 

     foreach (string fileName in GetFileNames()) 
     { 
      foreach (string folderPath in GetFolderPaths()) 
      { 
       // do your logic here 
      } 
     } 
     }