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>//< 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)!