不废话,直接代码
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();
}
}