I have the following function which I got online, and have changed to suit my needs :-
privateBoolean SheetExists(string strFilename)
Boolean Match = false;
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilename + ";Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(connString);
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String excelSheets = newString[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
excelSheets[i] = row["TABLE_NAME"].ToString();
for (int j = 0; j < excelSheets.Length; j++)
if (excelSheets[j] == "Sheet1$")
Match = true;
if (Match == true)
if (objConn != null)
if (dt != null)
Basically it checks if sheet1 exists in a workbook. I'm currently using the variable "Match" to obtain a "return true" or a "return false". I'm sure it possible to modify the code so that it returns the true/false directly, I just can't figure out how. Any ideas? Thanks
Basically, I can't figure out how to check for the correct sheet names based on the Switch. There will never be more than 3 sheets to check for, but there might be less than 3 - hence the null. Sheet1Name, etc are used in other functions, so I'd like to keep them... Any thoughts?
See my previous posting. Also, this has a problem. If Sheet2Name != null then the checking for Sheet1Name is ignored! (Exists is reset to false!) Likewise if Sheet3Name != null then the checking for Sheet1Name and Sheet2Name are ignored. This also, requires 3 loops through the dt.Rows.
Out of curiosity (as I'll be using your approach) if Sheet2Name != null why is checking for Sheet1Name ignored?
As I read it (if I have 1 sheetname to check), Sheet 1 is checked and Exists is set to true. Sheet2Name and Sheet3Name will be Null, and therefore their loops will be skipped, leaving Exists at true. No?
I'm assuming that you need to check if all of the non-null sheet names are present (as I said in the previous posting). Your first posting with the Sheet?Name variables indicated that 1, 2 or all 3 Sheet?Name variables could be non-null.
So you first check if the Sheet1Name is present, if so, you set Exists = true. Then, if Sheet2Name != null the first thing you do is set Exists = false. At this point you have no information about whether Sheet1Name was present or not. You are effectively ignoring the Sheet1Name status.
You can easily remedy this by adding Exists && before the Sheet2Name != null (and Sheet3Name case). This implicitly propagates the previous sheet name checking to the next checking step. It also, avoids pointless effort if an earlier step fails.
I think I see the problem. The code I gave was to check if any of Sheet1Name, Sheet2Name or Sheet3Name are in the DB. Are you trying to check if all of non-null values of Sheet1Name, Sheet2Name or Sheet3Name are represented as rows in the DB? If that is the case, you will need more complicated checking and keeping track of matching each of the sheet names separately. Try something like:
The code as you have shown it will throw a NullReferenceException if any of Sheet1Name, Sheet2Name or Sheet3Name are null. You can use the ?? operator to make this safe: