VB.NET - Programatically check to see if a Sheet exists in Excel
I was wondering if there is a way to programatically check and see if a sheet exsists in an excel workbook?
Basically what I am doing is checking if an excel document exists, opening it up and checking if the specified sheet exi开发者_如何学JAVAsts. I am currently unaware as to how to check and see if the sheet exists. Any help would be awesome! Thanks.
UPDATE
I have been given some good code to make this work. All i am missing now is the correct imports.
I have searched and found this import :
Imports Tools = Microsoft.Office.Tools.Excel
But for some reason that is not recognized. Do I have to configure VS somehow to make it work? Or am I just using the wrong import?
You can get the worksheets collection and iterate and check the name, this is c#
foreach (Sheet xlsSheet in xlsxWorkbooks)
{
if (xlsSheet.Name.equals("NameYouAreLookingFor")) // Maybe add ToLower() incase of case issues
{
//Return First Cell Value
}
}
again my example is in C# but this should get what you want.
bool sheetExist = false;
OleDbConnection objConn = new OleDbConnection(connString);
objConn.Open();
DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
if (dt.Rows.Count > 0)
{
DataRow[] rows = dt.Select("TABLE_NAME = 'Sheet1$'")
if (rows != null)
sheetExist = rows.count > 0
}
var containsSheet =
(workBookIn.Sheets.Cast<object>()
.Select(sheetValue => sheetValue as Worksheet))
.Any(workbookSheet => workbookSheet != null && workbookSheet.Name == sheetName);
精彩评论