diff --git a/App.config b/App.config
new file mode 100644
index 0000000..aee9adf
--- /dev/null
+++ b/App.config
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/App.xaml b/App.xaml
new file mode 100644
index 0000000..74a86bf
--- /dev/null
+++ b/App.xaml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/App.xaml.cs b/App.xaml.cs
new file mode 100644
index 0000000..f13a95d
--- /dev/null
+++ b/App.xaml.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Windows;
+using ExcelHelper.Services;
+using ExcelHelper.Utils;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+namespace ExcelHelper
+{
+ ///
+ /// App.xaml 的交互逻辑
+ ///
+ public partial class App : Application
+ {
+
+ public static App AppHost;
+ private readonly IHost _host = Host.CreateDefaultBuilder()
+ .AddViewAndViewModel()
+ .ConfigureServices((context, services) =>
+ {
+ services.AddHostedService();
+ services.AddSingleton();
+
+
+ })
+ .Build();
+ protected override void OnStartup(StartupEventArgs e)
+ {
+ AppHost = this;
+
+ base.OnStartup(e);
+
+ // 初始化AppHost
+ _host.Start();
+ }
+
+ public T Get()
+ {
+ return _host.Services.GetService();
+ }
+ public object Get(Type type)
+ {
+ return _host.Services.GetService(type);
+ }
+ }
+}
diff --git a/ExcelHelper.csproj b/ExcelHelper.csproj
new file mode 100644
index 0000000..0da09ee
--- /dev/null
+++ b/ExcelHelper.csproj
@@ -0,0 +1,57 @@
+
+
+ net9.0-windows
+ WinExe
+ publish\
+ true
+ Disk
+ false
+ Foreground
+ 7
+ Days
+ false
+ false
+ true
+ 0
+ 1.0.0.%2a
+ false
+ false
+ true
+ 12.0
+ false
+ true
+ true
+
+
+
+ False
+ Microsoft .NET Framework 4.8.1 %28x86 和 x64%29
+ true
+
+
+ False
+ .NET Framework 3.5 SP1
+ false
+
+
+
+
+ 8.3.2
+
+
+ 3.5.1
+
+
+
+ 1.34.2
+
+
+ 1.14.1
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+
+
+
+
\ No newline at end of file
diff --git a/ExcelHelper.sln b/ExcelHelper.sln
new file mode 100644
index 0000000..a09320b
--- /dev/null
+++ b/ExcelHelper.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.11.35327.3
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExcelHelper", "ExcelHelper.csproj", "{C7A7E24D-513F-4D66-9953-42EDBD0038B7}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {C7A7E24D-513F-4D66-9953-42EDBD0038B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C7A7E24D-513F-4D66-9953-42EDBD0038B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C7A7E24D-513F-4D66-9953-42EDBD0038B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C7A7E24D-513F-4D66-9953-42EDBD0038B7}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {F92EF236-3F64-4327-8CF6-C6FDBDC7939E}
+ EndGlobalSection
+EndGlobal
diff --git a/MainWindow.xaml b/MainWindow.xaml
new file mode 100644
index 0000000..e5dfeb2
--- /dev/null
+++ b/MainWindow.xaml
@@ -0,0 +1,58 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
new file mode 100644
index 0000000..9351b3a
--- /dev/null
+++ b/MainWindow.xaml.cs
@@ -0,0 +1,37 @@
+using System.Windows;
+using CommunityToolkit.Mvvm.Messaging;
+using ExcelHelper.Message;
+using ExcelHelper.Services;
+using ExcelHelper.Views;
+using ExcelHelper.Views.ViewModels;
+
+namespace ExcelHelper
+{
+ ///
+ /// MainWindow.xaml 的交互逻辑
+ ///
+ public partial class MainWindow : Window, IView
+ {
+ private MainViewModel ViewModel;
+ public MainWindow(MainViewModel viewModel, NavigationService navigationService)
+ {
+ InitializeComponent();
+ DataContext = ViewModel = viewModel;
+ navigationService.InitForFrame(MainFrame);
+ WeakReferenceMessenger.Default.Register(this, (r, message) =>
+ {
+ if (message.Value != null)
+ {
+ ViewModel.ErrorMessage = message.Value;
+ MessageDialog.Visibility = Visibility.Visible;
+ }
+ });
+ }
+
+ private void DialogCloseBtn_Click(object sender, System.Windows.RoutedEventArgs e)
+ {
+ MessageDialog.Visibility = System.Windows.Visibility.Collapsed;
+ }
+
+ }
+}
diff --git a/Message/ErrorDialogMessage.cs b/Message/ErrorDialogMessage.cs
new file mode 100644
index 0000000..8afbe4e
--- /dev/null
+++ b/Message/ErrorDialogMessage.cs
@@ -0,0 +1,9 @@
+using CommunityToolkit.Mvvm.Messaging.Messages;
+
+namespace ExcelHelper.Message;
+internal class ErrorDialogMessage : ValueChangedMessage
+{
+ public ErrorDialogMessage(string value) : base(value)
+ {
+ }
+}
diff --git a/Message/UpdateDataGridColumnsMessage.cs b/Message/UpdateDataGridColumnsMessage.cs
new file mode 100644
index 0000000..d844e70
--- /dev/null
+++ b/Message/UpdateDataGridColumnsMessage.cs
@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+using System.Windows.Controls;
+using CommunityToolkit.Mvvm.Messaging.Messages;
+
+namespace ExcelHelper.Message;
+internal class UpdateDataGridColumnsMessage : ValueChangedMessage>
+{
+ public UpdateDataGridColumnsMessage(List value) : base(value)
+ {
+
+
+ }
+}
diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs
new file mode 100644
index 0000000..754c871
--- /dev/null
+++ b/Properties/Resources.Designer.cs
@@ -0,0 +1,71 @@
+//------------------------------------------------------------------------------
+//
+// 此代码由工具生成。
+// 运行时版本: 4.0.30319.42000
+//
+// 对此文件的更改可能导致不正确的行为,如果
+// 重新生成代码,则所做更改将丢失。
+//
+//------------------------------------------------------------------------------
+
+namespace ExcelHelper.Properties
+{
+
+
+ ///
+ /// 强类型资源类,用于查找本地化字符串等。
+ ///
+ // 此类是由 StronglyTypedResourceBuilder
+ // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
+ // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
+ // (以 /str 作为命令选项),或重新生成 VS 项目。
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class Resources
+ {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal Resources()
+ {
+ }
+
+ ///
+ /// 返回此类使用的缓存 ResourceManager 实例。
+ ///
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager
+ {
+ get
+ {
+ if ((resourceMan == null))
+ {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ExcelHelper.Properties.Resources", typeof(Resources).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ ///
+ /// 重写当前线程的 CurrentUICulture 属性,对
+ /// 使用此强类型资源类的所有资源查找执行重写。
+ ///
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture
+ {
+ get
+ {
+ return resourceCulture;
+ }
+ set
+ {
+ resourceCulture = value;
+ }
+ }
+ }
+}
diff --git a/Properties/Resources.resx b/Properties/Resources.resx
new file mode 100644
index 0000000..af7dbeb
--- /dev/null
+++ b/Properties/Resources.resx
@@ -0,0 +1,117 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
\ No newline at end of file
diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs
new file mode 100644
index 0000000..4647dfe
--- /dev/null
+++ b/Properties/Settings.Designer.cs
@@ -0,0 +1,30 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+namespace ExcelHelper.Properties
+{
+
+
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
+ internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
+ {
+
+ private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
+
+ public static Settings Default
+ {
+ get
+ {
+ return defaultInstance;
+ }
+ }
+ }
+}
diff --git a/Properties/Settings.settings b/Properties/Settings.settings
new file mode 100644
index 0000000..033d7a5
--- /dev/null
+++ b/Properties/Settings.settings
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Services/ExcelHelperHostedService.cs b/Services/ExcelHelperHostedService.cs
new file mode 100644
index 0000000..4bf73a5
--- /dev/null
+++ b/Services/ExcelHelperHostedService.cs
@@ -0,0 +1,27 @@
+using System.Threading;
+using System.Threading.Tasks;
+using ExcelHelper.Views.Pages;
+using Microsoft.Extensions.Hosting;
+
+namespace ExcelHelper.Services
+{
+ public class ExcelHelperHostedService : IHostedService
+ {
+ public Task StartAsync(CancellationToken cancellationToken)
+ {
+ var mainWindow = App.AppHost.Get();
+ if (mainWindow != null)
+ {
+ mainWindow.Show();
+ App.AppHost.Get().NavigateTo();
+ }
+
+ return Task.CompletedTask;
+ }
+ public Task StopAsync(CancellationToken cancellationToken)
+ {
+ return Task.CompletedTask;
+
+ }
+ }
+}
diff --git a/Services/NavigationService.cs b/Services/NavigationService.cs
new file mode 100644
index 0000000..97f82fe
--- /dev/null
+++ b/Services/NavigationService.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Windows.Controls;
+using ExcelHelper.Views;
+
+namespace ExcelHelper.Services
+{
+ public class NavigationService
+ {
+ private Frame _frame;
+ public static string currentView;
+
+ public void InitForFrame(Frame frame)
+ {
+ _frame = frame;
+ }
+ public void NavigateTo()
+ {
+ var target = App.AppHost.Get();
+ if (target is IView page)
+ {
+ currentView = typeof(T).Name;
+ _frame.NavigationService.Navigate(page);
+ }
+ }
+ public void NavigateTo(Type type)
+ {
+ var target = App.AppHost.Get(type);
+ if (target is IView page)
+ {
+ currentView = type.Name;
+ _frame.NavigationService.Navigate(page);
+ }
+ }
+ }
+}
diff --git a/Utils/HostExtension.cs b/Utils/HostExtension.cs
new file mode 100644
index 0000000..b39ad5e
--- /dev/null
+++ b/Utils/HostExtension.cs
@@ -0,0 +1,32 @@
+using System.Reflection;
+using ExcelHelper.Views;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+namespace ExcelHelper.Utils
+{
+ internal static class HostExtension
+ {
+ public static IHostBuilder AddViewAndViewModel(this IHostBuilder hostBuilder)
+ {
+ // Scan all assemblies to find all types that implement IViewModel
+ var assem = Assembly.GetExecutingAssembly();
+ var types = assem.GetTypes();
+ foreach (var type in types)
+ {
+ if (type.IsInterface || type.IsAbstract)
+ {
+ continue;
+ }
+ if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type))
+ {
+ hostBuilder.ConfigureServices((context, services) =>
+ {
+ services.AddSingleton(type);
+ });
+ }
+ }
+ return hostBuilder;
+ }
+ }
+}
diff --git a/Utils/PasswordBoxHelper.cs b/Utils/PasswordBoxHelper.cs
new file mode 100644
index 0000000..dde7c02
--- /dev/null
+++ b/Utils/PasswordBoxHelper.cs
@@ -0,0 +1,83 @@
+using System.Windows;
+using System.Windows.Controls;
+
+namespace ExcelHelper.Utils;
+
+
+public static class PasswordBoxHelper
+{
+
+ public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper),
+ new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
+ public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, OnAttachPropertyChanged));
+
+ private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(PasswordBoxHelper));
+
+
+ public static void SetAttach(DependencyObject dp, bool value)
+ {
+ dp.SetValue(AttachProperty, value);
+ }
+
+ public static bool GetAttach(DependencyObject dp)
+ {
+ return (bool)dp.GetValue(AttachProperty);
+ }
+
+ public static string GetPassword(DependencyObject dp)
+ {
+ return (string)dp.GetValue(PasswordProperty);
+ }
+
+ public static void SetPassword(DependencyObject dp, string value)
+ {
+ dp.SetValue(PasswordProperty, value);
+ }
+
+ private static bool GetIsUpdating(DependencyObject dp)
+ {
+ return (bool)dp.GetValue(IsUpdatingProperty);
+ }
+
+ private static void SetIsUpdating(DependencyObject dp, bool value)
+ {
+ dp.SetValue(IsUpdatingProperty, value);
+ }
+
+ private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
+ {
+ PasswordBox passwordBox = sender as PasswordBox;
+ passwordBox.PasswordChanged -= PasswordChanged;
+ if (!(bool)GetIsUpdating(passwordBox))
+ {
+ passwordBox.Password = (string)e.NewValue;
+ }
+ passwordBox.PasswordChanged += PasswordChanged;
+ }
+
+ private static void OnAttachPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
+ {
+ PasswordBox passwordBox = sender as PasswordBox;
+ if (passwordBox == null)
+ {
+ return;
+ }
+ if ((bool)e.OldValue)
+ {
+ passwordBox.PasswordChanged -= PasswordChanged;
+ }
+ if ((bool)e.NewValue)
+ {
+ passwordBox.PasswordChanged += PasswordChanged;
+ }
+ }
+
+ private static void PasswordChanged(object sender, RoutedEventArgs e)
+ {
+ PasswordBox passwordBox = sender as PasswordBox;
+ SetIsUpdating(passwordBox, true);
+ SetPassword(passwordBox, passwordBox.Password);
+ SetIsUpdating(passwordBox, false);
+ }
+}
+
diff --git a/Views/IView.cs b/Views/IView.cs
new file mode 100644
index 0000000..2e7759d
--- /dev/null
+++ b/Views/IView.cs
@@ -0,0 +1,6 @@
+namespace ExcelHelper.Views
+{
+ internal interface IView
+ {
+ }
+}
diff --git a/Views/IViewModel.cs b/Views/IViewModel.cs
new file mode 100644
index 0000000..4db56bc
--- /dev/null
+++ b/Views/IViewModel.cs
@@ -0,0 +1,6 @@
+namespace ExcelHelper.Views
+{
+ internal interface IViewModel
+ {
+ }
+}
diff --git a/Views/Pages/DataListPage.xaml b/Views/Pages/DataListPage.xaml
new file mode 100644
index 0000000..d2fe59b
--- /dev/null
+++ b/Views/Pages/DataListPage.xaml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
diff --git a/Views/Pages/DataListPage.xaml.cs b/Views/Pages/DataListPage.xaml.cs
new file mode 100644
index 0000000..09a832e
--- /dev/null
+++ b/Views/Pages/DataListPage.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace ExcelHelper.Views.Pages
+{
+ ///
+ /// DataListPage.xaml 的交互逻辑
+ ///
+ public partial class DataListPage : Page, IView
+ {
+ public DataListPage()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/Views/Pages/ImportExcelPage.xaml b/Views/Pages/ImportExcelPage.xaml
new file mode 100644
index 0000000..959d857
--- /dev/null
+++ b/Views/Pages/ImportExcelPage.xaml
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Views/Pages/ImportExcelPage.xaml.cs b/Views/Pages/ImportExcelPage.xaml.cs
new file mode 100644
index 0000000..0ef22cd
--- /dev/null
+++ b/Views/Pages/ImportExcelPage.xaml.cs
@@ -0,0 +1,79 @@
+using System.Windows.Controls;
+using CommunityToolkit.Mvvm.Messaging;
+using ExcelHelper.Message;
+using ExcelHelper.Views.ViewModels;
+
+namespace ExcelHelper.Views.Pages
+{
+ ///
+ /// ImportExcelPage.xaml 的交互逻辑
+ ///
+ public partial class ImportExcelPage : Page, IView, IRecipient
+ {
+ private ImportViewModel ViewModel;
+ public ImportExcelPage(ImportViewModel viewModel)
+ {
+ InitializeComponent();
+ this.DataContext = ViewModel = viewModel;
+ //GenerateDataGridColumns();
+ //Messenger
+ ViewModel.IsActive = true;
+ // Register a message in some module
+ WeakReferenceMessenger.Default.Register(this, (r, message) =>
+ {
+ if (message.Value != null)
+ {
+ ExcelDataPreviewGrid.Columns.Clear();
+ foreach (var column in message.Value)
+ {
+ ExcelDataPreviewGrid.Columns.Add(column);
+ }
+ }
+ });
+ }
+ private void Grid_DragEnter(object sender, System.Windows.DragEventArgs e)
+ {
+ if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
+ {
+ e.Effects = System.Windows.DragDropEffects.Link;
+ }
+ else
+ {
+ e.Effects = System.Windows.DragDropEffects.None;
+ }
+ DropFileMask.Visibility = System.Windows.Visibility.Visible;
+ }
+
+ private void Grid_Drop(object sender, System.Windows.DragEventArgs e)
+ {
+ DropFileMask.Visibility = System.Windows.Visibility.Collapsed;
+ if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
+ {
+ var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
+ if (files.Length > 0)
+ {
+ //ViewModel.FileDropCommand.CanExecute(files);
+ ViewModel.FileDrop(files);
+ }
+ }
+ }
+
+ private void Grid_DragLeave(object sender, System.Windows.DragEventArgs e)
+ {
+ DropFileMask.Visibility = System.Windows.Visibility.Collapsed;
+ }
+
+ void IRecipient.Receive(UpdateDataGridColumnsMessage message)
+ {
+ if (message.Value != null)
+ {
+ ExcelDataPreviewGrid.Columns.Clear();
+ foreach (var column in message.Value)
+ {
+ ExcelDataPreviewGrid.Columns.Add(column);
+ }
+ }
+ }
+
+ }
+}
diff --git a/Views/ViewModels/ImportViewModel.cs b/Views/ViewModels/ImportViewModel.cs
new file mode 100644
index 0000000..9249c31
--- /dev/null
+++ b/Views/ViewModels/ImportViewModel.cs
@@ -0,0 +1,134 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Windows.Controls;
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Messaging;
+using ExcelHelper.Message;
+using MiniExcelLibs;
+
+namespace ExcelHelper.Views.ViewModels
+{
+ public partial class ImportViewModel : ObservableRecipient, IViewModel
+ {
+
+ public void FileDrop(string[] files)
+ {
+ ExcelFiles.Clear();
+ foreach (var item in files)
+ {
+ ExcelFiles.Add(item);
+ }
+ //ReadForExcel(ExcelFiles.First());
+ CurrentFilePath = ExcelFiles.First();
+ }
+
+ public void ReadForExcel(string path)
+ {
+ ExcelData = MiniExcel.Query(path, useHeaderRow: UseHeaderRow);
+ var columns = MiniExcel.GetColumns(path, useHeaderRow: UseHeaderRow);
+ GenColumns(columns);
+ }
+ private void GenColumns(IEnumerable columns)
+ {
+ Columns.Clear();
+ if (UseHeaderRow)
+ {
+
+ foreach (var columnName in columns)
+ {
+ var column = new DataGridTextColumn
+ {
+ Header = columnName,
+ Binding = new System.Windows.Data.Binding($"{columnName}"),
+ Width = DataGridLength.Auto
+ };
+ Columns.Add(column);
+ }
+ }
+ else
+ {
+ for (var i = 'A'; i < 'Z'; i++)
+ {
+ var column = new DataGridTextColumn
+ {
+ Header = $"{i}",
+ Binding = new System.Windows.Data.Binding($"{i}"),
+ Width = DataGridLength.Auto
+ };
+ Columns.Add(column);
+ }
+ }
+ if (Columns.Count > 0)
+ WeakReferenceMessenger.Default.Send(new UpdateDataGridColumnsMessage([.. Columns]));
+ else
+ {
+ WeakReferenceMessenger.Default.Send(new ErrorDialogMessage("未读取到列信息!"));
+ }
+ }
+ #region Props
+ [ObservableProperty]
+ private IEnumerable _excelData;
+
+ [ObservableProperty]
+ private ObservableCollection _excelFiles = [];
+
+ [ObservableProperty]
+ private ObservableCollection _columns = [];
+
+ ///
+ /// 使用首行作为表头
+ ///
+ [ObservableProperty]
+ private bool _useHeaderRow;
+
+ ///
+ /// 选中的Sheet
+ ///
+ [ObservableProperty]
+ private string _selectedSheetName;
+
+ ///
+ /// Sheets列表
+ ///
+ [ObservableProperty]
+ private ObservableCollection _sheets = [];
+ ///
+ /// 起始位置
+ ///
+ [ObservableProperty]
+ private string _startCell;
+ ///
+ /// 结束位置
+ ///
+ [ObservableProperty]
+ private string _endCell;
+ ///
+ /// 最大行数
+ ///
+ [ObservableProperty]
+ private string _maxRow;
+
+ ///
+ /// 当前文件路径
+ ///
+ private string _currentFilePath;
+ public string CurrentFilePath
+ {
+ get => _currentFilePath;
+ set
+ {
+ SetProperty(ref _currentFilePath, value);
+ ReadForExcel(value);
+ }
+ }
+
+ ///
+ /// Excel文件密码
+ ///
+ [ObservableProperty]
+ private string _excelPassword;
+
+ #endregion
+ }
+}
diff --git a/Views/ViewModels/MainViewModel.cs b/Views/ViewModels/MainViewModel.cs
new file mode 100644
index 0000000..6e7ad6b
--- /dev/null
+++ b/Views/ViewModels/MainViewModel.cs
@@ -0,0 +1,34 @@
+using System.Diagnostics;
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using ExcelHelper.Views.Pages;
+
+namespace ExcelHelper.Views.ViewModels
+{
+ public partial class MainViewModel : ObservableObject, IViewModel
+ {
+
+ ///
+ /// 异常信息
+ ///
+ [ObservableProperty]
+ private string _errorMessage;
+
+ [RelayCommand]
+ private void OnSideMenuSelect(string itemTag)
+ {
+ Trace.WriteLine($"OnSideMenuSelect -> {itemTag}");
+ var targetPage = itemTag switch
+ {
+ "ImportExcelPage" => typeof(ImportExcelPage),
+ "DataListPage" => typeof(DataListPage),
+ _ => null
+ };
+ if (targetPage != null)
+ {
+ App.AppHost.Get().NavigateTo(targetPage);
+ }
+ }
+
+ }
+}