简体   繁体   中英

How to get an existing range name from an Excel worksheet using c# Excel.Interop?

I am working with a very large worksheet that has many named ranges. I expect that the worksheet will be modified in the future, so I cannot interact with cells using their cell number.

How can I get an existing name (or list of the existing names) from the worksheet? Alternatively, how can I tell my C# program that a certain name already exists, and to utilize it?

You'll want to use Excel.Worksheet.Names

foreach (Excel.Worksheet sheet in wb.Sheets) {
    foreach (Excel.Range range in sheet.Names) {
        // this is the named ranges
     }
}

Source

If you were to use EPPlus , you could do this (very simple)

    static void Main(string[] args) {
        using (FileStream stream = new FileStream(@"C:\Users\bblack\Test\TestWorksheet.xlsx", FileMode.Append))  {
            using (ExcelPackage xl = new ExcelPackage(stream)) {
                // xl by default contains one workbook;
                bool test;
                foreach (ExcelWorksheet sheet in xl.Workbook.Worksheets) {
                    test = NamedRangeExists("NamedRange", sheet);
                }
            }
        }
    }

    static bool NamedRangeExists(string name, ExcelWorksheet ws) {
        return ws.Names.Where(n => n.Name == name).Count() > 0;
    }

That would open up the specified file, iterate through the sheets contained in the workbook and check for a named range within them called "NamedRange".

are you looking for something like this? In this solution I put them in a dictionary

Dictionary<string, Worksheet> dictionary = new Dictionary<string, Worksheet>();
foreach ( Worksheet worksheet in ws.Worksheets )
{
   dictionary.Add( worksheet.Name, worksheet );
}

update

Sheet1.Range("A3:A7")<-- get range

wb is of type Workbook

Dictionary<string, Range> dictionary = new Dictionary<string, Range>();
foreach (Worksheet sheet in wb.Sheets) {
    foreach (Range range in sheet.Names) {
        dictionary.Add( range.Name, Range );
        //use here range var
        var cells= range.Cells;
     }
}

refer here to see which property you want

This alternative to Ben Black's answer worked for me :

foreach (Excel.Worksheet sheet in wb.Sheets) {
    foreach (Excel.Name name in sheet.Names) {        
        var value = sheet.get_Range(name.NameLocal).Value;//get value of named range
        var formula = sheet.get_Range(name.NameLocal).Formula;//get Formula of named range
 }
}
        int row = 3;

        var wb = Globals.ThisAddIn.Application.ActiveWorkbook;
        ws.Range["A6"].Value = wb.Names.Count;

        foreach (Name rangeName in wb.Names)
        {
            Range c = ws.Cells[row++, 3];
            c.Value = rangeName.NameLocal;
        }
        var ws = Globals.ThisAddIn.Application.ActiveSheet;
        ws.Range["A7"].Value = ws.Range["MyRangeName"].Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM