我正在运行以下代码,如果我逐步执行代码,它会显示我的值,但是当它到达
时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 { }
}
答案 0 :(得分: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()) {
...
}
}
}
}
}
}
此外,不要吞没带有空挡块的异常,否则如果出现问题你就不知道出了什么问题。至少应该打印或记录堆栈跟踪,以便找出问题所在。
答案 1 :(得分: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
}
}
}