C # Get the name of the Excel workbook sheet page (worksheet) Collection

Source: Internet
Author: User

         #region Get the Sheet page (worksheet) name collection in the Excel workbook///<summary>//Get an Excel workbook in the sheet page (sheet) Name collection///&LT         ;/summary>//<param name= "excelfile" >excel file name and path, eg:c:\users\jk\desktop\ import Test .xls</param>            <returns>sheet Page Name Collection </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 sour Ce= "+ 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\ ""; CreateConnection Object objconn = new OleDbConnection (connstring);                Open database connection objConn.Open ();                Get the data table including data schema dt = objconn.getoledbschematable (OleDbSchemaGuid.Tables, NULL);                if (dt = = null) {return null; } string[] Excelsheets = new String[dt.                Rows.Count];                int i = 0; Joins the sheet name to the string array foreach (DataRow row in dt. Rows) {String strsheettablename = row["table_name"].                    ToString (); Filter invalid SheetName if (Strsheettablename.contains ("$") &&strsheettablename.replace ("'", ""). EndsWith ("$")) {Excelsheets[i] = strsheettablename.substring (0, Strsheettablena Me.                    LENGTH-1);                } i++;            } return excelsheets;     }       catch (Exception ex) {MessageBox.Show (ex.                ToString ());            return null;                    } finally {//Cleans if (objconn! = null) {                    Objconn.close ();                Objconn.dispose (); } if (dt! = NULL) {dt.                Dispose (); }}} #endregion

Copyright notice: Author: jiankunking Resources: http://blog.csdn.net/jiankunking The author and csdn partner, welcome reprint. However, if the submit permission is not declared from this section, it must be persisted and connected to the original article page given the view location.

C # Get the name of the Excel workbook sheet page (worksheet) Collection

Related Article

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.