diff --git a/App.xaml.cs b/App.xaml.cs
index f13a95d..73111ac 100644
--- a/App.xaml.cs
+++ b/App.xaml.cs
@@ -5,42 +5,39 @@ using ExcelHelper.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace ExcelHelper
+namespace ExcelHelper;
+
+///
+/// App.xaml 的交互逻辑
+///
+public partial class App : Application
{
- ///
- /// 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;
- public static App AppHost;
- private readonly IHost _host = Host.CreateDefaultBuilder()
- .AddViewAndViewModel()
- .ConfigureServices((context, services) =>
- {
- services.AddHostedService();
- services.AddSingleton();
+ base.OnStartup(e);
+ // 初始化AppHost
+ _host.Start();
+ }
- })
- .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);
- }
+ public T Get()
+ {
+ return _host.Services.GetService();
+ }
+ public object Get(Type type)
+ {
+ return _host.Services.GetService(type);
}
}
diff --git a/Converter/BooleanConverter.cs b/Converter/BooleanConverter.cs
new file mode 100644
index 0000000..2d63223
--- /dev/null
+++ b/Converter/BooleanConverter.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Windows.Data;
+
+namespace ExcelHelper.Converter;
+public class BooleanConverter : IValueConverter
+{
+ protected BooleanConverter(T tValue, T fValue)
+ {
+ True = tValue;
+ False = fValue;
+ }
+
+ public T True
+ {
+ get; set;
+ }
+
+ public T False
+ {
+ get; set;
+ }
+
+
+ public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+ {
+ return value is bool flag && flag ? True : False;
+ }
+
+ public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+ {
+ return value is T flag && EqualityComparer.Default.Equals(flag, True);
+ }
+}
\ No newline at end of file
diff --git a/Converter/ReBooleanToVisibilityConverter.cs b/Converter/ReBooleanToVisibilityConverter.cs
new file mode 100644
index 0000000..21fbda8
--- /dev/null
+++ b/Converter/ReBooleanToVisibilityConverter.cs
@@ -0,0 +1,12 @@
+using System.Windows;
+using System.Windows.Data;
+
+namespace ExcelHelper.Converter;
+///
+/// BooleanToVisibilityConverter 反转
+///
+[ValueConversion(typeof(bool), typeof(Visibility))]
+public class ReBooleanToVisibilityConverter : BooleanConverter
+{
+ public ReBooleanToVisibilityConverter() : base(Visibility.Visible, Visibility.Collapsed) { }
+}
\ No newline at end of file
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 9351b3a..b3c6772 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -18,6 +18,7 @@ namespace ExcelHelper
InitializeComponent();
DataContext = ViewModel = viewModel;
navigationService.InitForFrame(MainFrame);
+
WeakReferenceMessenger.Default.Register(this, (r, message) =>
{
if (message.Value != null)
diff --git a/Utils/HostExtension.cs b/Utils/HostExtension.cs
index b39ad5e..661e871 100644
--- a/Utils/HostExtension.cs
+++ b/Utils/HostExtension.cs
@@ -3,30 +3,29 @@ using ExcelHelper.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
-namespace ExcelHelper.Utils
+namespace ExcelHelper.Utils;
+
+internal static class HostExtension
{
- internal static class HostExtension
+ public static IHostBuilder AddViewAndViewModel(this IHostBuilder hostBuilder)
{
- 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)
{
- // 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)
{
- if (type.IsInterface || type.IsAbstract)
- {
- continue;
- }
- if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type))
- {
- hostBuilder.ConfigureServices((context, services) =>
- {
- services.AddSingleton(type);
- });
- }
+ continue;
+ }
+ if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type))
+ {
+ hostBuilder.ConfigureServices((context, services) =>
+ {
+ services.AddSingleton(type);
+ });
}
- return hostBuilder;
}
+ return hostBuilder;
}
}
diff --git a/Views/Pages/ImportExcelPage.xaml b/Views/Pages/ImportExcelPage.xaml
index 959d857..1c09ed8 100644
--- a/Views/Pages/ImportExcelPage.xaml
+++ b/Views/Pages/ImportExcelPage.xaml
@@ -2,11 +2,12 @@
x:Class="ExcelHelper.Views.Pages.ImportExcelPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:converter="clr-namespace:ExcelHelper.Converter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:local="clr-namespace:ExcelHelper.Views.Pages"
- xmlns:utils="clr-namespace:ExcelHelper.Utils"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:utils="clr-namespace:ExcelHelper.Utils"
xmlns:viewmodels="clr-namespace:ExcelHelper.Views.ViewModels"
Title="ImportExcelPage"
d:DataContext="{d:DesignInstance Type=viewmodels:ImportViewModel}"
@@ -14,6 +15,8 @@
d:DesignWidth="1200"
mc:Ignorable="d">
+
+
+
+
+
+
+
+
+
@@ -39,16 +55,39 @@
Grid.Column="0"
Grid.ColumnSpan="2"
Panel.ZIndex="10"
+ Content="{Binding}"
ContentTemplate="{StaticResource Mask}"
Visibility="Collapsed" />
+
-
+
-
-
-
+
+
+
@@ -67,7 +106,10 @@
Margin="5,0"
VerticalAlignment="Center"
Text="参数配置" />
-
+
@@ -79,38 +121,49 @@
-
+
+ Command=""
+ Content="读取" />
-
+
-
+
-
+
-
+
-
+
-
+
diff --git a/Views/Pages/ImportExcelPage.xaml.cs b/Views/Pages/ImportExcelPage.xaml.cs
index 0ef22cd..e31c05e 100644
--- a/Views/Pages/ImportExcelPage.xaml.cs
+++ b/Views/Pages/ImportExcelPage.xaml.cs
@@ -15,8 +15,6 @@ namespace ExcelHelper.Views.Pages
{
InitializeComponent();
this.DataContext = ViewModel = viewModel;
- //GenerateDataGridColumns();
- //Messenger
ViewModel.IsActive = true;
// Register a message in some module
WeakReferenceMessenger.Default.Register(this, (r, message) =>
@@ -52,7 +50,6 @@ namespace ExcelHelper.Views.Pages
var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
if (files.Length > 0)
{
- //ViewModel.FileDropCommand.CanExecute(files);
ViewModel.FileDrop(files);
}
}
@@ -74,6 +71,5 @@ namespace ExcelHelper.Views.Pages
}
}
}
-
}
}
diff --git a/Views/ViewModels/ImportViewModel.cs b/Views/ViewModels/ImportViewModel.cs
index 9249c31..a307315 100644
--- a/Views/ViewModels/ImportViewModel.cs
+++ b/Views/ViewModels/ImportViewModel.cs
@@ -1,134 +1,205 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using ExcelHelper.Message;
using MiniExcelLibs;
-namespace ExcelHelper.Views.ViewModels
+namespace ExcelHelper.Views.ViewModels;
+
+
+public partial class ImportViewModel : ObservableRecipient, IViewModel
{
- public partial class ImportViewModel : ObservableRecipient, IViewModel
+
+ public void FileDrop(string[] files)
{
-
- public void FileDrop(string[] files)
+ IsLoading = true;
+ ExcelFiles.Clear();
+ SelectedSheetName = null;
+ Sheets.Clear();
+ MaxRow = 0;
+ foreach (var item in files)
{
- ExcelFiles.Clear();
- foreach (var item in files)
- {
- ExcelFiles.Add(item);
- }
- //ReadForExcel(ExcelFiles.First());
- CurrentFilePath = ExcelFiles.First();
+ ExcelFiles.Add(item);
}
-
- 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
+ //ReadForExcel(ExcelFiles.First());
+ CurrentFilePath = ExcelFiles.FirstOrDefault();
+ IsLoading = false;
}
+
+ public async void ReadForExcel(string path)
+ {
+ IsLoading = true;
+ //await Task.Factory.StartNew(async () =>
+ //{
+ try
+ {
+ if (string.IsNullOrEmpty(path)) return;
+ var excelData = await MiniExcel.QueryAsync(path, sheetName: SelectedSheetName, useHeaderRow: UseHeaderRow, startCell: StartCell);
+ if (MaxRow != 0)
+ {
+ ExcelData = excelData.Take(Math.Min(MaxRow, 300));
+ }
+ else
+ {
+ ExcelData = excelData.Take(300);
+ }
+ var columns = MiniExcel.GetColumns(path, useHeaderRow: UseHeaderRow, startCell: StartCell, sheetName: SelectedSheetName);
+ GenColumns(columns);
+ GetSheets(path);
+ MaxRow = ExcelData.Count();
+ }
+ catch (Exception ex)
+ {
+ WeakReferenceMessenger.Default.Send(new ErrorDialogMessage($"出现了异常!{ex}"));
+ }
+ IsLoading = false;
+ //}).ContinueWith(x =>
+ // {
+
+
+ // });
+ }
+ private void GenColumns(IEnumerable columns)
+ {
+ ExcelColumns.Clear();
+ if (UseHeaderRow)
+ {
+
+ foreach (var columnName in columns)
+ {
+ var column = new DataGridTextColumn
+ {
+ Header = columnName,
+ Binding = new System.Windows.Data.Binding($"{columnName}"),
+ Width = DataGridLength.Auto
+ };
+ ExcelColumns.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
+ };
+ ExcelColumns.Add(column);
+ }
+ }
+ if (ExcelColumns.Count > 0)
+ WeakReferenceMessenger.Default.Send(new UpdateDataGridColumnsMessage([.. ExcelColumns]));
+ else
+ {
+ WeakReferenceMessenger.Default.Send(new ErrorDialogMessage("未读取到列信息!"));
+ }
+ }
+
+ [RelayCommand]
+ private void OnReLoadExcel()
+ {
+ ReadForExcel(CurrentFilePath);
+ }
+ [RelayCommand]
+ private void OnReadSheets()
+ {
+ if (string.IsNullOrWhiteSpace(CurrentFilePath))
+ {
+ GetSheets(CurrentFilePath);
+ }
+ }
+ private void GetSheets(string excelPath)
+ {
+ if (Sheets == null)
+ {
+ Sheets = new ObservableCollection();
+ }
+ else
+ {
+ Sheets.Clear();
+ }
+ MiniExcel.GetSheetNames(excelPath)?.ForEach(sheetName =>
+ {
+ Sheets.Add(sheetName);
+ });
+ SelectedSheetName = Sheets.FirstOrDefault();
+ }
+
+ #region Props
+ [ObservableProperty]
+ private IEnumerable _excelData;
+
+ [ObservableProperty]
+ private ObservableCollection _excelFiles = [];
+
+ [ObservableProperty]
+ private ObservableCollection _excelColumns = [];
+
+ ///
+ /// 是否正在加载
+ ///
+ [ObservableProperty]
+ private bool _isLoading = false;
+
+ ///
+ /// 使用首行作为表头
+ ///
+ [ObservableProperty]
+ private bool _useHeaderRow = false;
+
+ ///
+ /// 选中的Sheet
+ ///
+ [ObservableProperty]
+ private string _selectedSheetName;
+
+ ///
+ /// Sheets列表
+ ///
+ [ObservableProperty]
+ private ObservableCollection _sheets = [];
+ ///
+ /// 起始位置
+ ///
+ [ObservableProperty]
+ private string _startCell = "A1";
+ ///
+ /// 结束位置
+ ///
+ [ObservableProperty]
+ private string _endCell;
+ ///
+ /// 最大行数
+ ///
+ [ObservableProperty]
+ private int _maxRow = 0;
+
+ ///
+ /// 当前文件路径
+ ///
+ private string _currentFilePath;
+ public string CurrentFilePath
+ {
+ get => _currentFilePath;
+ set
+ {
+ SetProperty(ref _currentFilePath, value);
+ ReadForExcel(value);
+ }
+ }
+
+ ///
+ /// Excel文件密码
+ ///
+ [ObservableProperty]
+ private string _excelPassword;
+
+ #endregion
}