简体   繁体   English

如何使用c#Excel.Interop从Excel工作表中获取现有范围名称?

[英]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? 或者,如何告诉我的C#程序某个名称已经存在,并加以利用?

You'll want to use Excel.Worksheet.Names 您将要使用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) 如果要使用EPPlus ,则可以执行此操作(非常简单)

    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". 这将打开指定的文件,遍历工作簿中包含的工作表,并检查其中的名为“ 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 Sheet1.Range(“ A3:A7”)<-获取范围

wb is of type Workbook wb是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 : 本·布莱克(Ben Black)的答案的替代方法对我有用:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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