#region Get the Sheet page (worksheet) name collection in the Excel workbook///<summary>//Get an Excel workbook in the sheet page (sheet) Name collection///< ;/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