class Program
{
static void Main(string[] args)
{
var result = GetDefinedNames(@"..\..\Documents\Sheet10.xlsx");
foreach (var dn in result)
Console.WriteLine("{0} {1}", dn.Key, dn.Value);
Console.ReadLine();
}
public static Dictionary GetDefinedNames(String fileName)
{
// Given a workbook name, return a dictionary of defined names.
// The pairs include the range name and a string representing the range.
var returnValue = new Dictionary();
// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
// Retrieve a reference to the workbook part.
var wbPart = document.WorkbookPart;
// Retrieve a reference to the defined names collection.
DefinedNames definedNames = wbPart.Workbook.DefinedNames;
// If there are defined names, add them to the dictionary.
if (definedNames != null)
{
foreach (DefinedName dn in definedNames)
returnValue.Add(dn.Name.Value, dn.Text);
}
}
return returnValue;
}
}
class Program
{
static void Main(string[] args)
{
string strDoc = @"..\..\Documents\Sheet10.xlsx";
Dictionary result = GetDefinedNames(strDoc);
foreach (var dn in result)
Console.WriteLine("{0} {1}", dn.Key, dn.Value);
Console.ReadLine();
}
public static Dictionary GetDefinedNames(String fileName)
{
var returnValue = new Dictionary();
//Initialize a new Workboook object
Workbook workbook = new Workbook();
//Load the document
workbook.LoadFromFile(fileName);
//Getting all named ranges
INameRanges range = workbook.NameRanges;
//If there are items in Ranges, add them to the dictionary.
if (range != null)
{
foreach (INamedRange rn in range)
returnValue.Add(rn.Name, rn.Value.ToString());
}
return returnValue;
}
}