我想强调焦点到特定的单元格,我有一个通过XlCall.Excel(XlCall.xlfCaller)的引用。我知道如何在VSTO中做到这一点,但有没有办法用ExcelDNA做到这一点?
答案 0 :(得分:2)
一种方法是使用COM Automation接口,就像使用VBA或VSTO一样。请务必使用从Application
获取的ExcelDnaUtil.Application
对象作为根。要从ExcelReference
收到的xlfCaller
转换为COM Range
对象,您可以尝试(这是VB.NET版本):
Private Function ReferenceToRange(ByVal xlRef As ExcelReference) As Object
Dim strAddress As String = XlCall.Excel(XlCall.xlfReftext, xlRef, True)
ReferenceToRange = ExcelDnaUtil.Application.Range(strAddress)
End Function
如果您想坚持使用C API,首先必须选择正确的工作表,然后选择实际的工作表。所以你可能有:
string myCellSheet = (string)XlCall.Excel(XlCall.xlSheetNm, myCell);
XlCall.Excel(XlCall.xlcWorkbookSelect, new object[] { myCellSheet });
XlCall.Excel(XlCall.xlcFormulaGoto, myCell);
您将无法更改工作表函数中的选择,因此我假设您是从宏或功能区处理程序中调用它。
答案 1 :(得分:1)
可以使用ExcelDNA:
var activeCell = new ExcelReference(5, 5);
ExcelAsyncUtil.QueueAsMacro(() => XlCall.Excel(XlCall.xlcSelect, activeCell));
可以跳过“ExcelAsyncUtil.QueueAsMacro”,它取决于您调用Excel命令的上下文。如果您从另一个Excel函数调用它 - 您应该使用QueueAsMacro
将其包装起来