C# - Get NamedRanges of a particular sheet in Excel using Oledb -
Using OLEDB, is it possible to get all NamedRanges of a particula sheet in Excel?
I have the code which gives me NamedRanges the following but I am not able to figure out what the sheet NamedRange refers to
Personal String [] GetExcelSheetNames (string excelFilePath) {OleDbConnection objConn = Null System.Data.DataTable.dt = Null; Try {// string connString = "provider = Microsoft.Jet.OLEDB.4.0; data source =" + excel file + "; extended property = excel 12.0;"; String connectionstring = String.Format ("provider = Microsoft.ACE.OLEDB.12.0; data source = {0}; extended property = excel 12.0", excelFilePath); ObjConn = New OleDbConnection (connectionString); ObjConn.Open (); // Get schema guide containg data table Dt = objConn.GetOleDbSchemaTable (OleDbSchemaGuid.Tables_Info, blank); If return (DT == zero) returns; String [] excelSheets = new string [dt.Rows.Count]; Int i = 0; // Add the name of the sheet to the string array. Forex Currency (DatRow line in dt.rows) Excelsheets [i ++] = Line ["TABLE_NAME"] ToString (); Return Excel Sheet; } Hold (ex before) {return tap; } Finally {// clear if (objConn! = Null) {objConn.Close (); ObjConn.Dispose (); } If (dt! = Null) {dt.Dispose (); }}}}}I am an open XML SDK fan, the solution is simple, both of these workbooks And sheet scoped gives named ranges, on the left there are two names in Excel named Manager Definitions, 2 sheets with each of the named ranges, a sample run on the right.
![]()
// / & LT; Summary & gt; The /// process checks the workbook you are told, /// is looking for part with defined names. /// If this is present, then /// for each defined name by adding name and value to the returned word, it repeats the process through all part of the /// content /// & lt; / Summary & gt; Public static IDictionary & lt; String, string & gt; XLGetDefinedNames (string filename) {var returnValue = New dictionary & lt; String, string & gt; (); // spreadsheet document document = spreadsheet document. (Filename, false) using {var wbPart = document.WorkbookPart; // Define name defined name = wbPart.Workbook.DefinedNames; If (definedNames = null!) {Foreach (definedNames in DefinedName dn) returnValue.Add (dn.Name.Value, dn.Text); }} // return return value; }
Comments
Post a Comment