diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 13a0f25..258f544 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -82,161 +82,35 @@ public partial class MainWindow : Window #region 菜单点击按钮 - // 在 MainWindow 内部 - /// - /// 按钮点击触发文件页面 - /// - /// - /// + private void OnFilePageButtonClick(object sender, RoutedEventArgs e) { - FilePage filePage = new FilePage(); - NavigationWindow navWin = new NavigationWindow(); - - navWin.Width = 810; - navWin.Height = 600; - navWin.ResizeMode = ResizeMode.NoResize; - navWin.Content = filePage; - navWin.ShowsNavigationUI = false; - navWin.Title = "记录列表"; - - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600); } - - /// - /// 按钮点击触发配置页面 - /// - /// - /// private void OnConfigPageButtonClick(object sender, RoutedEventArgs e) { - ConfigPage configPage = new ConfigPage(); - NavigationWindow navWin = new NavigationWindow(); - navWin.Content = configPage; - navWin.ShowsNavigationUI = false; - navWin.Width = 972; - navWin.Height = 648; - navWin.Title = "配置"; - navWin.ResizeMode = ResizeMode.NoResize; - - - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new ConfigPage(), "配置", 972, 648); } - /// - /// 按钮点击触发标准基准页面 - /// - /// - /// private void OnStandardPageButtonClick(object sender, RoutedEventArgs e) { - StandardPage standardPage = new StandardPage(); - NavigationWindow navWin = new NavigationWindow(); - navWin.Width = 972; - navWin.Height = 648; - //navWin.ResizeMode = ResizeMode.NoResize; - navWin.Content = standardPage; - navWin.ShowsNavigationUI = false; - navWin.Title = "主标定"; - - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new StandardPage(), "主标定", 972, 648, isResizable: true); } - /// - /// 按钮点击触发Gauge页面 - /// - /// - /// private void OnGaugePageButtonClick(object sender, RoutedEventArgs e) { - GaugePage gaugePage = new GaugePage(); - NavigationWindow navWin = new NavigationWindow(); - navWin.Content = gaugePage; - navWin.ShowsNavigationUI = false; - navWin.Width = 1080; - navWin.Height = 720; - navWin.Title = "Gauge R&&R"; - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new GaugePage(), "Gauge R&&R", 1080, 720); } - - /// - /// 按钮点击触发CgCgk页面 - /// - /// - /// private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) { - CgCgkPage cgCgkPage = new CgCgkPage(); - NavigationWindow navWin = new NavigationWindow(); - navWin.Content = cgCgkPage; - navWin.ShowsNavigationUI = false; - navWin.Title = "CgCgk"; - - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new CgCgkPage(), "CgCgk", 972, 648, isResizable: true); } - - /// - /// 按钮点击触发系统设置页面 - /// - /// - /// private void OnSysSetPageButtonClick(object sender, RoutedEventArgs e) { - SysSetPage sysSetPage = new SysSetPage(); - NavigationWindow navWin = new NavigationWindow(); - navWin.Width = 810; - navWin.Height = 600; - navWin.ResizeMode = ResizeMode.NoResize; - navWin.Content = sysSetPage; - navWin.ShowsNavigationUI = false; - navWin.Title = "系统设置"; - - - // ✅ 新增:设定父窗口、样式与居中弹出 - navWin.Owner = Application.Current.MainWindow; // 绑定主窗口 - navWin.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 居中弹出 - navWin.WindowStyle = WindowStyle.ToolWindow; - - // ✅ 改这里:Show() 改为 ShowDialog() 实现模态窗口 - navWin.ShowDialog(); + WindowHelper.ShowPageDialog(new SysSetPage(), "系统设置", 810, 600); } #endregion @@ -253,7 +127,6 @@ public partial class MainWindow : Window } - /// /// 测试按钮 /// diff --git a/Utils/ExcelHelper.cs b/Utils/ExcelHelper.cs new file mode 100644 index 0000000..d795f6f --- /dev/null +++ b/Utils/ExcelHelper.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.IO; +using unvell.ReoGrid; + +namespace WpfApp.Utils +{ + /// + /// Excel 操作静态工具类(基于 ReoGrid) + /// + public static class ExcelHelper + { + private static ReoGridControl reoGridControl; + private static Worksheet currentSheet; + + static ExcelHelper() + { + reoGridControl = new ReoGridControl(); + } + + /// + /// 从模板加载 Excel 文件 + /// + public static void LoadTemplate(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"模板文件不存在: {filePath}"); + + reoGridControl.Load(filePath); + currentSheet = reoGridControl.CurrentWorksheet; + } + + /// + /// 切换到指定工作表 + /// + public static void SelectSheet(string sheetName) + { + if (reoGridControl == null) + throw new InvalidOperationException("请先调用 LoadTemplate 方法加载文件。"); + + var sheet = reoGridControl.Worksheets[sheetName]; + if (sheet == null) + throw new ArgumentException($"未找到名为 '{sheetName}' 的工作表。"); + + currentSheet = sheet; + reoGridControl.CurrentWorksheet = sheet; + } + + /// + /// 获取当前工作表名称 + /// + public static string GetCurrentSheetName() + { + return currentSheet?.Name ?? string.Empty; + } + + /// + /// 读取单元格 + /// + public static object ReadCell(string cellName) + { + return currentSheet?.GetCellData(cellName); + } + + /// + /// 设置单元格值 + /// + public static void WriteCell(string cellName, object value) + { + currentSheet?.SetCellData(cellName, value); + } + + /// + /// 批量写入单元格 + /// + public static void WriteCells(Dictionary cellValues) + { + foreach (var kv in cellValues) + { + currentSheet?.SetCellData(kv.Key, kv.Value); + } + } + + /// + /// 在指定行插入数据(从 startColumn 开始) + /// + public static void InsertRowData(int rowIndex, int startColumn, IList values) + { + if (currentSheet == null) + throw new InvalidOperationException("请先加载模板并选择工作表。"); + + for (int i = 0; i < values.Count; i++) + { + currentSheet[rowIndex, startColumn + i] = values[i]; + } + } + + /// + /// 保存为新文件 + /// + public static void SaveAs(string savePath) + { + if (reoGridControl == null) + throw new InvalidOperationException("请先调用 LoadTemplate 方法加载文件。"); + + reoGridControl.Save(savePath, unvell.ReoGrid.IO.FileFormat.Excel2007); + } + + /// + /// 打开 Excel 文件(并显示到 ReoGrid 控件) + /// + /// Excel 文件路径 + /// 用于显示的 ReoGridControl 容器 + public static void OpenExcel(string filePath, ReoGridControl reoGridHost) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"文件不存在: {filePath}"); + + // 替换当前控件引用 + reoGridControl?.Dispose(); + reoGridControl = reoGridHost; + + // 加载 Excel 文件 + reoGridControl.Load(filePath); + + // 默认显示第一个工作表 + currentSheet = reoGridControl.CurrentWorksheet; + } + + + /// + /// 清理资源 + /// + public static void Dispose() + { + reoGridControl?.Dispose(); + reoGridControl = null; + currentSheet = null; + } + } +} diff --git a/Utils/WindowHelper.cs b/Utils/WindowHelper.cs new file mode 100644 index 0000000..97f5805 --- /dev/null +++ b/Utils/WindowHelper.cs @@ -0,0 +1,34 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Navigation; + +namespace WpfApp.Utils; + +public static class WindowHelper +{ + /// + /// 通用窗口打开方法 + /// + /// 要显示的页面实例 + /// 窗口标题 + /// 窗口宽度 + /// 窗口高度 + /// 是否允许调整大小 + public static void ShowPageDialog(Page page, string title, double width = 900, double height = 600, bool isResizable = false) + { + var navWin = new NavigationWindow + { + Content = page, + Title = title, + Width = width, + Height = height, + ShowsNavigationUI = false, + WindowStartupLocation = WindowStartupLocation.CenterOwner, + WindowStyle = WindowStyle.ToolWindow, + Owner = Application.Current.MainWindow, + ResizeMode = isResizable ? ResizeMode.CanResize : ResizeMode.NoResize + }; + + navWin.ShowDialog(); + } +} diff --git a/WpfApp.csproj b/WpfApp.csproj index 2536c12..f3b24e2 100644 --- a/WpfApp.csproj +++ b/WpfApp.csproj @@ -40,6 +40,7 @@ + @@ -49,6 +50,7 @@ + diff --git a/src/public/Excel/Report_2025_10_13.xlsx b/src/public/Excel/Report_2025_10_13.xlsx new file mode 100644 index 0000000..535e947 Binary files /dev/null and b/src/public/Excel/Report_2025_10_13.xlsx differ diff --git a/src/public/Excel/demo.xlsx b/src/public/Excel/demo.xlsx new file mode 100644 index 0000000..850ae1e Binary files /dev/null and b/src/public/Excel/demo.xlsx differ diff --git a/src/view/CgCgkPage.xaml b/src/view/CgCgkPage.xaml index e8f41c8..bbb0558 100644 --- a/src/view/CgCgkPage.xaml +++ b/src/view/CgCgkPage.xaml @@ -1,6 +1,7 @@  - -