C # Get a collection of sheet page (sheet) names in an Excel workbook

Source: Internet
Author: User
C # Get a collection of sheet page (sheet) names in an Excel workbook

  #region Get the Sheet page (worksheet) name collection in the Excel workbook///<summary>//Get an Excel workbook in the sheet page (sheet) Name collection///</summa ry>//<param name= "excelfile" >excel file name and path, eg:c:\users\jk\desktop\ Import Test .xls</param>//&LT Returns>sheet Page Name Collection </returns> private string[] Getexcelsheetnames (String fileName) {Ol            Edbconnection 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\ "";    Create a Connection object             objconn = new OleDbConnection (connstring);                Open database connection objConn.Open ();                Get the data table containing the data schema dt = objconn.getoledbschematable (OleDbSchemaGuid.Tables, NULL);                if (dt = = null) {return null; } string[] Excelsheets = new String[dt.                Rows.Count];                int i = 0; Adds a worksheet name to a string array of 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

The above is C # get the sheet page (sheet) name collection in the Excel workbook, and more about topic.alibabacloud.com (www.php.cn)!

  • 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.