using System.Collections.Generic; namespace DataCompare.Core { public sealed class ExportTable { public string SheetTitle { get; set; } public string Label { get; set; } public string Status { get; set; } public string Note { get; set; } public CompareResult Result { get; set; } public bool Completed { get; set; } } public sealed class XlsxSheet { public string Name; public readonly List Rows = new List(); } public static class DiffWorkbook { public const int MaxRows = 1048576; public static bool Include(RowDiff diff, int filter) { if (diff == null) return false; switch (filter) { case 1: return diff.Status == DiffStatus.LeftOnly; case 2: return diff.Status == DiffStatus.RightOnly; case 3: return diff.Status == DiffStatus.Different; case 4: return diff.Status == DiffStatus.DuplicateKey || diff.Status == DiffStatus.InvalidKey; default: return true; } } public static List Build(IList tables, int filter) { return Build(tables, filter, MaxRows); } public static List Build(IList tables, int filter, int maxRows) { if (maxRows < 2) maxRows = 2; var sheets = new List(); var used = new List(); var summary = Begin(used, "汇总"); summary.Rows.Add(new[] { "表", "状态", "左侧行数", "右侧行数", "相同", "仅左", "仅右", "内容不同", "键重复", "键无效", "说明" }); if (tables != null) { for (int i = 0; i < tables.Count; i++) { var table = tables[i]; if (table == null) continue; if (summary.Rows.Count >= maxRows) { sheets.Add(summary); summary = Begin(used, "汇总"); summary.Rows.Add(new[] { "表", "状态", "左侧行数", "右侧行数", "相同", "仅左", "仅右", "内容不同", "键重复", "键无效", "说明" }); } summary.Rows.Add(SummaryRow(table)); } } sheets.Add(summary); if (tables != null) { for (int i = 0; i < tables.Count; i++) { var table = tables[i]; if (table == null || !table.Completed || table.Result == null) continue; var sheet = Begin(used, table.SheetTitle); sheet.Rows.Add(DiffHeader()); var diffs = table.Result.Diffs; if (diffs != null) { for (int d = 0; d < diffs.Length; d++) { if (!Include(diffs[d], filter)) continue; AddDiff(sheets, used, ref sheet, table.SheetTitle, diffs[d], maxRows); } } sheets.Add(sheet); } } return sheets; } static void AddDiff( List sheets, List used, ref XlsxSheet sheet, string title, RowDiff diff, int maxRows) { if (diff.Cells == null || diff.Cells.Length == 0) { sheet = Room(sheets, used, sheet, title, maxRows); sheet.Rows.Add(new[] { Text(diff.StatusText), Text(diff.KeyText), Text(diff.Summary), "", "", "", "" }); return; } for (int c = 0; c < diff.Cells.Length; c++) { var cell = diff.Cells[c]; sheet = Room(sheets, used, sheet, title, maxRows); if (cell == null) { sheet.Rows.Add(new[] { Text(diff.StatusText), Text(diff.KeyText), Text(diff.Summary), "", "", "", "" }); continue; } sheet.Rows.Add(new[] { Text(diff.StatusText), Text(diff.KeyText), Text(diff.Summary), Text(cell.ColumnName), Text(cell.LeftText), Text(cell.RightText), cell.IsDifferent ? "是" : "否" }); } } static XlsxSheet Room(List sheets, List used, XlsxSheet sheet, string title, int maxRows) { if (sheet.Rows.Count < maxRows) return sheet; sheets.Add(sheet); var next = Begin(used, title); next.Rows.Add(DiffHeader()); return next; } static string[] DiffHeader() { return new[] { "状态", "键", "摘要", "列", "左侧", "右侧", "是否不同" }; } static string[] SummaryRow(ExportTable table) { var result = table.Completed ? table.Result : null; return new[] { Text(table.Label), Text(table.Status), result == null ? "" : result.LeftRowCount.ToString(), result == null ? "" : result.RightRowCount.ToString(), result == null ? "" : result.MatchedCount.ToString(), result == null ? "" : result.LeftOnlyCount.ToString(), result == null ? "" : result.RightOnlyCount.ToString(), result == null ? "" : result.DifferentCount.ToString(), result == null ? "" : result.DuplicateCount.ToString(), result == null ? "" : result.InvalidCount.ToString(), Text(table.Note) }; } static XlsxSheet Begin(List used, string title) { return new XlsxSheet { Name = SheetNames.Make(title, used) }; } static string Text(string value) { return value ?? ""; } } }