使用Excel的COM对象模型,如何激活工作表?我正在寻找替代???的地方在下面的C#代码中。
// Note the currently active sheet.
object originalActiveSheet = activeWindow.ActiveSheet;
// Activate the target worksheet.
targetWorksheet.Activate();
// Do stuff that requires the target worksheet to be active.
// ...
// Re-activate the previously active sheet.
// ???
更新:
经过进一步研究,这是我能做的最好的事情:
// Re-activate the previously active sheet.
if (originalActiveSheet is Worksheet)
((Worksheet)originalActiveSheet).Activate();
else if (originalActiveSheet is Chart)
((Chart)originalActiveSheet).Activate();
else if (originalActiveSheet is DialogSheet)
((DialogSheet)originalActiveSheet).Activate();
else
throw new Exception("Unable to re-activate the previously active sheet");
似乎是一个不完整的解决方案。有没有人有更好的东西?