///
// This mehtodd retrieves the Excel sheet names from
// an Excel Workbook.
///
/// the Excel file.
// string []
private string [] getexcelsheetnames (string excelfile)
{< br> oledbconnection objconn = NULL;
system. data. datatable dt = NULL;
try
{< br> // connection string. change the Excel file to the file you
// will search.
string connstring = "provider = Microsoft. jet. oledb.4.0; "+
" Data Source = "+ excelfile +"; extended properties = Excel 8.0; ";
// create connection object by using the preceding connection string.
objconn = new oledbconnection (connstring);
// open connection with the database.
objconn. open ();
// get the data table containg the schema guid.
dt = objconn. getoledbschematable (oledbschemaguid. tables, null);
If (Dt = NULL)
{< br> return NULL;
}
String [] excelsheets = new string [DT. Rows. Count];
Int I = 0;
// Add the sheet name to the string array.
Foreach (datarow row in DT. Rows)
{
Excelsheets [I] = row ["table_name"]. tostring ();
I ++;
}
// Loop through all of the sheets if you want too...
For (Int J = 0; j <excelsheets. length; j ++)
{
// Query each Excel sheet.
}
return excelsheets;
}< br> catch (exception ex)
{< br> return NULL;
}< br> finally
{< br> // clean up.
If (objconn! = NULL)
{< br> objconn. Close ();
objconn. Dispose ();
}< br> If (DT! = NULL)
{< br> DT. Dispose ();
}< BR >}