diff --git a/src/SopHelper.Avalonia/SopHelper.Avalonia.csproj b/src/SopHelper.Avalonia/SopHelper.Avalonia.csproj
index 2c1c7e4..59f90af 100644
--- a/src/SopHelper.Avalonia/SopHelper.Avalonia.csproj
+++ b/src/SopHelper.Avalonia/SopHelper.Avalonia.csproj
@@ -20,6 +20,8 @@
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
index 22db0c6..23b62ce 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
@@ -118,6 +118,8 @@
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopExcelExport.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopExcelExport.cs
new file mode 100644
index 0000000..55cbe03
--- /dev/null
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopExcelExport.cs
@@ -0,0 +1,67 @@
+using System.IO;
+using ClosedXML.Excel;
+
+namespace SopHelper.VVMs.RecipeEditor;
+
+/// 供 SOP 表格导出线程读取的步骤快照。
+internal sealed record SopExcelRow(int Id, string Name, string Description, string KeyFrameBase64);
+
+/// 生成供查阅的 SOP 工作簿,并在生成成功后替换目标文件。
+internal static class SopExcelExport
+{
+ /// 将步骤按执行顺序写入指定路径,关键帧随工作簿保存。
+ public static void Save(string path, IReadOnlyList steps)
+ {
+ using var workbook = new XLWorkbook();
+ var sheet = workbook.AddWorksheet("SOP");
+ sheet.Cell("A1").Value = "步骤号";
+ sheet.Cell("B1").Value = "步骤名";
+ sheet.Cell("C1").Value = "步骤说明";
+ sheet.Cell("D1").Value = "关键帧";
+ var header = sheet.Range("A1:D1");
+ header.Style.Font.Bold = true;
+ header.Style.Fill.BackgroundColor = XLColor.LightBlue;
+ sheet.SheetView.FreezeRows(1);
+ sheet.Column(1).Width = 12;
+ sheet.Column(2).Width = 28;
+ sheet.Column(3).Width = 65;
+ sheet.Column(4).Width = 42;
+ sheet.Columns(2, 3).Style.Alignment.WrapText = true;
+ sheet.Columns(1, 4).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
+
+ var row = 2;
+ foreach (var step in steps.OrderBy(step => step.Id))
+ {
+ sheet.Cell(row, 1).Value = step.Id;
+ sheet.Cell(row, 2).Value = step.Name;
+ sheet.Cell(row, 3).Value = step.Description;
+ sheet.Row(row).AdjustToContents(2, 3);
+ if (!string.IsNullOrWhiteSpace(step.KeyFrameBase64))
+ {
+ // 以原图比例限制显示尺寸,按 96 DPI 将图片高度换算成工作表行高。
+ using var image = new MemoryStream(Convert.FromBase64String(step.KeyFrameBase64));
+ var picture = sheet.AddPicture(image);
+ var scale = Math.Min(1, Math.Min(260.0 / picture.Width, 150.0 / picture.Height));
+ var width = Math.Max(1, (int)Math.Round(picture.Width * scale));
+ var height = Math.Max(1, (int)Math.Round(picture.Height * scale));
+ picture.MoveTo(sheet.Cell(row, 4), 4, 4).WithSize(width, height);
+ sheet.Row(row).Height = Math.Max(sheet.Row(row).Height, (height + 8) * .75);
+ }
+ row++;
+ }
+
+ // 在目标目录写临时文件,避免生成失败时截断已有工作簿。
+ var temporary = Path.Combine(Path.GetDirectoryName(path)!, $".sop-export-{Guid.NewGuid():N}.tmp");
+ try
+ {
+ using (var stream = File.Create(temporary))
+ workbook.SaveAs(stream);
+ File.Move(temporary, path, overwrite: true);
+ }
+ finally
+ {
+ try { File.Delete(temporary); }
+ catch (Exception error) when (error is IOException or UnauthorizedAccessException) { }
+ }
+ }
+}
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
index 5d38476..107641d 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
@@ -1,4 +1,4 @@
-// 编辑器的步骤交换视图部分:处理文件导入/导出、追加或替换选择以及导入结果提示。
+// 编辑器的步骤交换视图部分:处理步骤交换与 SOP 表格的文件选择、结果提示。
using Avalonia.Controls;
using Avalonia.Interactivity;
@@ -67,6 +67,35 @@ public partial class RecipeEditor
}
}
+ /// 选择 Excel 保存位置并导出当前编辑中的步骤,与步骤交换共用防重入门控。
+ private async void ExportSopFile(object? sender, RoutedEventArgs args)
+ {
+ if (_stepTransferInProgress || _model is not { CanTransferSteps: true } model || TopLevel.GetTopLevel(this) is not Window owner) return;
+ _stepTransferInProgress = true;
+ try
+ {
+ var name = string.Concat(model.Recipe.Name.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c));
+ var file = await owner.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
+ {
+ Title = "导出SOP文件", SuggestedFileName = name + ".xlsx", DefaultExtension = "xlsx", ShowOverwritePrompt = true,
+ FileTypeChoices = [new("Excel 工作簿") { Patterns = ["*.xlsx"] }]
+ });
+ if (file is null || _closing || !ReferenceEquals(model, _model) || !model.CanTransferSteps) return;
+ var path = file.TryGetLocalPath() ?? throw new IOException("请选择本地保存位置");
+ if (!Path.GetExtension(path).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
+ throw new IOException("请使用 .xlsx 文件扩展名");
+ if (model.IsCurrentRecipeFile(path)) throw new IOException("SOP 导出不能覆盖当前配方文件");
+
+ // 在 UI 线程固定步骤数据,后台写文件时不再访问可编辑节点。
+ var steps = model.Recipe.Steps.Select(step => new SopExcelRow(step.Id, step.Name, step.Description, step.KeyFrameBase64)).ToArray();
+ await Task.Run(() => SopExcelExport.Save(path, steps));
+ if (!_closing && ReferenceEquals(model, _model))
+ model.StepTransferStatus = $"已导出 {steps.Length} 个步骤到 Excel:{Path.GetFileName(path)}";
+ }
+ catch (Exception error) { await ShowNotice("导出SOP文件失败", error.Message, false); }
+ finally { _stepTransferInProgress = false; }
+ }
+
private async Task ChooseStepImportMode(int incoming, int current)
{
if (_closing || _dialog is not null || TopLevel.GetTopLevel(this) is not Window owner) return null;
diff --git a/tests/VideoFlowTest.Smoke/Program.cs b/tests/VideoFlowTest.Smoke/Program.cs
index 13dcdf0..e551aa5 100644
--- a/tests/VideoFlowTest.Smoke/Program.cs
+++ b/tests/VideoFlowTest.Smoke/Program.cs
@@ -34,6 +34,8 @@ internal static class Program
if (args.Contains("--live")) { LiveVideoFlowChecks.Run(args); return; }
if (args.Contains("--agent-generation-only")) { AgentDraftGenerationChecks.Run(root); return; }
if (args.Contains("--optimization-only")) { OptimizeStepsChecks.Run(); return; }
+ // 单独验证步骤交换和 Excel 导出,无需先执行视频判定用例。
+ if (args.Contains("--step-transfer-only")) { StepTransferChecks.Run(root); return; }
CheckLegacyRecipeRoundTrip();
// 单独验证标准视频链路,便于定位回放与流程回测问题。
if (args.Contains("--standard-video-only"))
diff --git a/tests/VideoFlowTest.Smoke/StepTransferChecks.cs b/tests/VideoFlowTest.Smoke/StepTransferChecks.cs
index 2698d0f..93ac2eb 100644
--- a/tests/VideoFlowTest.Smoke/StepTransferChecks.cs
+++ b/tests/VideoFlowTest.Smoke/StepTransferChecks.cs
@@ -1,6 +1,7 @@
using System.Text.Json;
using Avalonia.Controls;
using Avalonia.VisualTree;
+using ClosedXML.Excel;
using Prism.Dialogs;
using SopHelper.Engine.Sop;
using SopHelper.VVMs.RecipeEditor;
@@ -61,8 +62,11 @@ internal static class StepTransferChecks
var window = new Window { Content = rendered, Width = 1040, Height = 900 }; window.Show(); window.UpdateLayout();
var tabs = rendered.GetVisualDescendants().OfType().Single();
tabs.SelectedItem = tabs.Items.OfType().Single(t => t.Header?.ToString() == "步骤"); window.UpdateLayout();
- Check(rendered.GetVisualDescendants().OfType