代码封装&增加Excel帮助类
This commit is contained in:
parent
0a2798497f
commit
4f1f1a7259
|
|
@ -82,161 +82,35 @@ public partial class MainWindow : Window
|
|||
|
||||
|
||||
#region 菜单点击按钮
|
||||
// 在 MainWindow 内部
|
||||
/// <summary>
|
||||
/// 按钮点击触发文件页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按钮点击触发配置页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按钮点击触发标准基准页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按钮点击触发Gauge页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按钮点击触发CgCgk页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按钮点击触发系统设置页面
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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
|
|||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 测试按钮
|
||||
/// </summary>
|
||||
|
|
|
|||
141
Utils/ExcelHelper.cs
Normal file
141
Utils/ExcelHelper.cs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using unvell.ReoGrid;
|
||||
|
||||
namespace WpfApp.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Excel 操作静态工具类(基于 ReoGrid)
|
||||
/// </summary>
|
||||
public static class ExcelHelper
|
||||
{
|
||||
private static ReoGridControl reoGridControl;
|
||||
private static Worksheet currentSheet;
|
||||
|
||||
static ExcelHelper()
|
||||
{
|
||||
reoGridControl = new ReoGridControl();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从模板加载 Excel 文件
|
||||
/// </summary>
|
||||
public static void LoadTemplate(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
throw new FileNotFoundException($"模板文件不存在: {filePath}");
|
||||
|
||||
reoGridControl.Load(filePath);
|
||||
currentSheet = reoGridControl.CurrentWorksheet;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换到指定工作表
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前工作表名称
|
||||
/// </summary>
|
||||
public static string GetCurrentSheetName()
|
||||
{
|
||||
return currentSheet?.Name ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取单元格
|
||||
/// </summary>
|
||||
public static object ReadCell(string cellName)
|
||||
{
|
||||
return currentSheet?.GetCellData(cellName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置单元格值
|
||||
/// </summary>
|
||||
public static void WriteCell(string cellName, object value)
|
||||
{
|
||||
currentSheet?.SetCellData(cellName, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量写入单元格
|
||||
/// </summary>
|
||||
public static void WriteCells(Dictionary<string, object> cellValues)
|
||||
{
|
||||
foreach (var kv in cellValues)
|
||||
{
|
||||
currentSheet?.SetCellData(kv.Key, kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在指定行插入数据(从 startColumn 开始)
|
||||
/// </summary>
|
||||
public static void InsertRowData(int rowIndex, int startColumn, IList<object> values)
|
||||
{
|
||||
if (currentSheet == null)
|
||||
throw new InvalidOperationException("请先加载模板并选择工作表。");
|
||||
|
||||
for (int i = 0; i < values.Count; i++)
|
||||
{
|
||||
currentSheet[rowIndex, startColumn + i] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存为新文件
|
||||
/// </summary>
|
||||
public static void SaveAs(string savePath)
|
||||
{
|
||||
if (reoGridControl == null)
|
||||
throw new InvalidOperationException("请先调用 LoadTemplate 方法加载文件。");
|
||||
|
||||
reoGridControl.Save(savePath, unvell.ReoGrid.IO.FileFormat.Excel2007);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开 Excel 文件(并显示到 ReoGrid 控件)
|
||||
/// </summary>
|
||||
/// <param name="filePath">Excel 文件路径</param>
|
||||
/// <param name="reoGridHost">用于显示的 ReoGridControl 容器</param>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 清理资源
|
||||
/// </summary>
|
||||
public static void Dispose()
|
||||
{
|
||||
reoGridControl?.Dispose();
|
||||
reoGridControl = null;
|
||||
currentSheet = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Utils/WindowHelper.cs
Normal file
34
Utils/WindowHelper.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Navigation;
|
||||
|
||||
namespace WpfApp.Utils;
|
||||
|
||||
public static class WindowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用窗口打开方法
|
||||
/// </summary>
|
||||
/// <param name="page">要显示的页面实例</param>
|
||||
/// <param name="title">窗口标题</param>
|
||||
/// <param name="width">窗口宽度</param>
|
||||
/// <param name="height">窗口高度</param>
|
||||
/// <param name="isResizable">是否允许调整大小</param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -40,6 +40,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.205" />
|
||||
<PackageReference Include="System.Management" Version="8.0.0" />
|
||||
<PackageReference Include="unvell.ReoGridWPF.dll" Version="3.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
@ -49,6 +50,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="src\public\Excel\" />
|
||||
<Folder Include="新文件夹\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
BIN
src/public/Excel/Report_2025_10_13.xlsx
Normal file
BIN
src/public/Excel/Report_2025_10_13.xlsx
Normal file
Binary file not shown.
BIN
src/public/Excel/demo.xlsx
Normal file
BIN
src/public/Excel/demo.xlsx
Normal file
Binary file not shown.
|
|
@ -1,6 +1,7 @@
|
|||
<Page x:Class="WpfApp.src.view.CgCgkPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:rg="clr-namespace:unvell.ReoGrid;assembly=unvell.ReoGrid"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:WpfApp.src.view"
|
||||
|
|
@ -8,8 +9,20 @@
|
|||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="CgCgkPage">
|
||||
|
||||
<Grid>
|
||||
<Button Content="CgCgkPage"/>
|
||||
<Grid x:Name="MainGrid">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<!-- 按钮行,高度自适应 -->
|
||||
<RowDefinition Height="*"/>
|
||||
<!-- Excel控件行,填满剩余空间 -->
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 按钮 -->
|
||||
<Button Grid.Row="0" Content="操作 Excel" Click="OnCgCgkPageButtonClick" HorizontalAlignment="Left" VerticalAlignment="Top" />
|
||||
|
||||
<!-- Excel 显示控件 -->
|
||||
<rg:ReoGridControl Grid.Row="1" x:Name="grid"/>
|
||||
</Grid>
|
||||
|
||||
|
||||
</Page>
|
||||
|
|
|
|||
|
|
@ -1,20 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
namespace WpfApp.src.view;
|
||||
using WpfApp.Utils;
|
||||
using unvell.ReoGrid;
|
||||
|
||||
|
||||
namespace WpfApp.src.view
|
||||
{
|
||||
/// <summary>
|
||||
/// CgCgkPage.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
|
|
@ -24,5 +15,33 @@ namespace WpfApp.src.view
|
|||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
string templatePath = @"C:\Users\yongye\Desktop\code\code\SolartronMetrologyWpfApp\WpfApp\src\public\Excel\demo.xlsx";
|
||||
string savePath = @"C:\Users\yongye\Desktop\code\code\SolartronMetrologyWpfApp\WpfApp\src\public\Excel\Report_2025_10_13.xlsx";
|
||||
|
||||
// 1️⃣ 加载模板
|
||||
ExcelHelper.LoadTemplate(templatePath);
|
||||
|
||||
// 2️⃣ 选择工作表
|
||||
ExcelHelper.SelectSheet("表2");
|
||||
|
||||
// 3️⃣ 插入数据
|
||||
ExcelHelper.InsertRowData(5, 0, new List<object> { "传感器1", 25.6, 101.3, DateTime.Now });
|
||||
|
||||
// 4️⃣ 修改单元格
|
||||
ExcelHelper.WriteCell("B2", "测试人员");
|
||||
ExcelHelper.WriteCell("C3", DateTime.Now.ToString("yyyy-MM-dd"));
|
||||
|
||||
// 5️⃣ 保存为新文件
|
||||
ExcelHelper.SaveAs(savePath);
|
||||
|
||||
// 6️⃣ 在页面控件上显示保存后的 Excel
|
||||
grid.Load(savePath); // 这里直接加载 Report_2025_10_13.xlsx
|
||||
|
||||
// 7️⃣ 不要 Dispose,否则控件被销毁
|
||||
// ExcelHelper.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user