VSTO 撤回代码修改的值

  • VSTO 撤回代码修改的值已关闭评论
  • 55 次浏览
  • A+
所属分类:.NET技术

不废话,直接代码

private Stack<Action> actionStack = new Stack<Action>();  private void SetCellValues() {     var worksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;      var range = worksheet.Range["A1"];     var previousValue = range.Value2;      // 保存当前状态以便恢复     actionStack.Push(() =>     {         range.Value2 = previousValue;     });  }  private void UndoLastOperation() {     if (actionStack.Count > 0)     {         var undoAction = actionStack.Pop();         undoAction();     } }