c# - Excel C# : How to get the Worksheet/Workbook name and address from Range object?

标签 c# excel

我注册了一个 Excel.Application.SheetChange 事件,该事件传递更改的范围对象。

我正在尝试通过尽可能少的互操作调用从中提取工作表名称、工作簿名称和地址。

大家有什么好主意吗?例如。就像我怎样才能获得该范围的完整地址,然后进行一些字符串解析以将其取出?

这是代码片段:

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

非常感谢。

最佳答案

您应该查看各种属性:

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

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

如果您确实想沿着解析路线走下去,那么您可以通过在 Address 索引器中指定 External = true 来获取该范围的完全限定地址:

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

请参阅documentation for Address了解详情。

关于c# - Excel C# : How to get the Worksheet/Workbook name and address from Range object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775892/

相关文章:

python - 创建动态工作表变量

java - 使用 POI 恢复单元格中的粗体文本

excel - 使用 DAX 的表达式中的 "starts with"

Excel 动态范围,如连接、计算行

c# - Gridview 页面索引仅在需要时更新

c# - 如何更改特定元素的 XML 命名空间

c# - 多项式评估的生成方法

java - 使用 poi 自动调整列宽

c# - 是否有将模型转换为 View 模型的快捷方式?

c# - 使用 linq 选择字典列表