[英]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.