Obtain the sheet name in Excel

Source: Internet
Author: User
///
/// This mehtodd retrieves the Excel sheet names from
/// An Excel Workbook.
///
/// The Excel file.
/// String []
Private string [] getexcelsheetnames (string excelfile)
{
Oledbconnection objconn = NULL;
System. Data. datatable dt = NULL; try
{
// 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)
{
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;
}
Catch (exception ex)
{
Return NULL;
}
Finally
{
// Clean up.
If (objconn! = NULL)
{
Objconn. Close ();
Objconn. Dispose ();
}
If (DT! = NULL)
{
DT. Dispose ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.