简体   繁体   English

Excel C#:如何从Range对象获取工作表/工作簿名称和地址?

[英]Excel C#: How to get the Worksheet/Workbook name and address from Range object?

I have a Excel.Application.SheetChange event that I register to which passes the range object that changed. 我注册了一个Excel.Application.SheetChange事件,并将更改的范围对象传递给该事件。

I am trying to extract the Worksheet name, Workbook name and address from that with as little interop calls as possible. 我试图通过尽可能少的互操作调用从中提取工作表名称,工作簿名称和地址。

Does anyone have any good ideas? 有人有什么好主意吗? Eg Like how can I get the full address of the the range and then do some string parsing to get it out? 例如,我如何获取范围的完整地址 ,然后进行一些字符串解析以将其取出?

Here is the code snippit: 这是代码片段:

    private void ws_change(Excel.Range target){
        //Code to get target workbook name, worksheet name, and range address
   }

Many thanks. 非常感谢。

You should look at the various properties: 您应该查看各种属性:

var sheet = range.Worksheet;
var sheetName = sheet.Name;

var workbook = (Workbook)sheet.Parent;
var workbookName = workbook.Name;

If you really want to go down the parsing route, then you can get the fully qualified address of the range by specifying External = true in the Address indexer: 如果您确实想沿解析路线走,那么可以通过在Address索引器中指定External = true来获得范围的标准Address

var address = range.Address[true, true, XlReferenceStyle.xlA1, true, null];
// returns e.g. [Book1]Sheet1!A1

See the documentation for Address for details. 有关详细信息,请参见地址文档

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

相关问题 如何使用c#Excel.Interop从Excel工作表中获取现有范围名称? - How to get an existing range name from an Excel worksheet using c# Excel.Interop? Excel-Dna C# 如何从 Excel 工作表中的选定范围获取值 - Excel-Dna C# How to get values from selected range in excel worksheet Excel / C#-通过名称获取工作表ID? - Excel/C# - Get worksheet id by name? 如何将ac#对象附加到excel工作表并在保存工作簿时保存它 - how to attach a c# object to an excel worksheet and have it saved when the workbook is saved C#Excel:从其他工作簿复制工作表时出现问题 - C# Excel: Issue in copying worksheet from different workbook C#-如何在当前工作簿中保存一个Excel WorkSheet? - C# - How to save a single Excel WorkSheet in the current WorkBook? 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# C# - 如何将单个 Excel 工作表从一个工作簿复制到另一个工作簿? - C# - How to copy a single Excel worksheet from one workbook to another? 在 C# 中引用 Excel 文件、工作簿、工作表 - Referencing an Excel file, workbook, worksheet in C# 如何在Excel的Excel.Range对象中选择工作表中的所有单元格? - How to Select all the cells in a worksheet in Excel.Range object of c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM