Obtain the sheet page (worksheet) name set in the Excel worksheet

Source: Internet
Author: User
# Region get the sheet page (worksheet) name set in the Excel worksheet /// <summary> // get the sheet page (worksheet) in the Excel worksheet) name set // </Summary> /// <Param name = "excelfile"> Excel file name and path, eg: C: \ Users \ JK \ Desktop \ import test .xls </param> // <returns> Sheet Page name set </returns> private string [] getexcelsheetnames (string filename) {oledbconnection objconn = NULL; system. data. datatable dt = NULL; try {string connstring = string. empty; string filetype = filename. substring (Filename. lastindexof (". "); If (filetype = ". xls ") connstring =" provider = Microsoft. jet. oledb.4.0; "+" Data Source = "+ filename +"; extended properties = Excel 8.0; "; else //. XLSX connstring = "provider = Microsoft. ace. oledb.12.0; "+" Data Source = "+ filename +"; "+"; extended properties = \ "Excel 12.0; HDR = yes; IMEX = 1 \""; // create the connection object objconn = new oledbconnection (connstring); // open the database connection objconn. open ();// The data table dt = objconn containing the data architecture is obtained. getoledbschematable (oledbschemaguid. tables, null); If (Dt = NULL) {return NULL;} string [] excelsheets = new string [DT. rows. count]; int I = 0; // Add the worksheet name to the string array foreach (datarow row in DT. rows) {string strsheettablename = row ["table_name"]. tostring (); // invalid sheetname if (strsheettablename. contains ("$") & strsheettablename. replace ("'",""). endswith ("$") {excelsheets [I] = Strsheettablename. substring (0, strsheettablename. length-1);} I ++;} return excelsheets;} catch (exception ex) {MessageBox. show (ex. tostring (); return NULL;} finally {// clear if (objconn! = NULL) {objconn. Close (); objconn. Dispose ();} If (DT! = NULL) {DT. Dispose () ;}}# endregion

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.