Skip to content

Commit 0fc3c6c

Browse files
committed
添加月份数据导出Excel功能
1 parent 738a99a commit 0fc3c6c

File tree

5 files changed

+70
-11
lines changed

5 files changed

+70
-11
lines changed

src/Local/ProjectEye/ProjectEye.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@
470470
<PackageReference Include="Newtonsoft.Json">
471471
<Version>12.0.2</Version>
472472
</PackageReference>
473+
<PackageReference Include="Npoi.Mapper">
474+
<Version>3.5.1</Version>
475+
</PackageReference>
473476
<PackageReference Include="SQLite.CodeFirst">
474477
<Version>1.5.3.29</Version>
475478
</PackageReference>

src/Local/ProjectEye/Resources/Language/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,6 @@
208208
<sys:String x:Key="Lang_DataWindowRestTimeImagePath">Rest time placeholder image</sys:String>
209209
<sys:String x:Key="Lang_DataWindowSkipImagePath">Skip placeholder image</sys:String>
210210
<sys:String x:Key="Lang_StatisticsWindow">Statistics window</sys:String>
211+
<sys:String x:Key="Lang_ExportMonthData">Export month data</sys:String>
211212

212213
</ResourceDictionary>

src/Local/ProjectEye/Resources/Language/zh.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,6 @@
214214
<sys:String x:Key="Lang_DataWindowRestTimeImagePath">休息时间占位图片</sys:String>
215215
<sys:String x:Key="Lang_DataWindowSkipImagePath">跳过次数占位图片</sys:String>
216216
<sys:String x:Key="Lang_StatisticsWindow">数据统计窗口</sys:String>
217+
<sys:String x:Key="Lang_ExportMonthData">导出本月数据</sys:String>
217218

218219
</ResourceDictionary>

src/Local/ProjectEye/ViewModels/StatisticViewModel.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Project1.UI.Controls.ChartControl.Models;
1+
using Npoi.Mapper;
2+
using Project1.UI.Controls.ChartControl.Models;
23
using Project1.UI.Cores;
34
using ProjectEye.Core;
45
using ProjectEye.Core.Service;
@@ -26,6 +27,8 @@ public class StatisticViewModel
2627

2728
public Command CloseOnboardingCommand { get; set; }
2829
public Command GenerateMonthlyDataImgCommand { get; set; }
30+
public Command exportDataCommand { get; set; }
31+
2932

3033
public StatisticViewModel(
3134
StatisticService statistic,
@@ -40,6 +43,8 @@ public StatisticViewModel(
4043

4144
CloseOnboardingCommand = new Command(new Action<object>(OnCloseOnboardingCommand));
4245
GenerateMonthlyDataImgCommand = new Command(new Action<object>(OnGenerateMonthlyDataImgCommand));
46+
exportDataCommand = new Command(new Action<object>(OnExportDataCommand));
47+
4348
Data = new StatisticModel();
4449
Data.Year = DateTime.Now.Year;
4550
Data.Month = DateTime.Now.Month;
@@ -65,14 +70,16 @@ public StatisticViewModel(
6570
LoadImages();
6671
}
6772

73+
74+
6875
private void LoadImages()
6976
{
7077
string worktimeimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowWorkTimeImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/web_developer.png" : config.options.Style.DataWindowWorkTimeImagePath;
7178
string resttimeimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowRestTimeImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/coffee_lover.png" : config.options.Style.DataWindowRestTimeImagePath;
7279
string skipimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowSkipImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/office_work_.png" : config.options.Style.DataWindowSkipImagePath;
7380

7481
Data.WorktimeImageSource = BitmapImager.Load(worktimeimgpath);
75-
Data.ResttimeImageSource= BitmapImager.Load(resttimeimgpath);
82+
Data.ResttimeImageSource = BitmapImager.Load(resttimeimgpath);
7683
Data.SkipImageSource = BitmapImager.Load(skipimgpath);
7784

7885
}
@@ -429,5 +436,41 @@ private void OnGenerateMonthlyDataImgCommand(object obj)
429436
Data.MonthWork).Generate();
430437
}
431438
}
439+
public struct XlsxData
440+
{
441+
442+
}
443+
private void OnExportDataCommand(object obj)
444+
{
445+
try
446+
{
447+
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
448+
dlg.FileName = "Project Eye statistic data " + Data.Year + Data.Month;
449+
dlg.DefaultExt = ".xlsx";
450+
dlg.Filter = "(.xlsx)|*.xlsx";
451+
Nullable<bool> result = dlg.ShowDialog();
452+
if (result == true)
453+
{
454+
// 获取选择月份的数据
455+
var monthData = statistic.GetData(Data.Year, Data.Month);
456+
457+
// 重新构建友好结构
458+
459+
var mapper = new Mapper();
460+
mapper
461+
.Map<Core.Models.Statistic.StatisticModel>("Date 日期", o => o.Date)
462+
.Map<Core.Models.Statistic.StatisticModel>("Work(hours) 工作(小时)", o => o.WorkingTime)
463+
.Map<Core.Models.Statistic.StatisticModel>("Rest(minutes) 休息(分钟)", o => o.ResetTime)
464+
.Map<Core.Models.Statistic.StatisticModel>("Skip 跳过(次)", o => o.SkipCount)
465+
.Ignore<Core.Models.Statistic.StatisticModel>(o => o.ID)
466+
.Save(dlg.FileName, monthData, $"{Data.Year}{Data.Month}", overwrite: true);
467+
}
468+
}
469+
catch (Exception e)
470+
{
471+
LogHelper.Error(e.Message);
472+
MessageBox.Show("导出数据失败,了解详情请查看错误日志");
473+
}
474+
}
432475
}
433476
}

src/Local/ProjectEye/Views/StatisticWindow.xaml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
3232
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />-->
3333
</Grid>
34-
34+
3535
</ControlTemplate>
3636
</Setter.Value>
3737
</Setter>
3838
<Style.Resources>
3939
<DropShadowEffect x:Key="CardShadow" BlurRadius="12" Direction="0" ShadowDepth="0" Opacity=".3" RenderingBias="Performance" ></DropShadowEffect>
4040

4141
</Style.Resources>
42-
42+
4343
<Style.Triggers>
4444
<MultiTrigger>
4545
<MultiTrigger.Conditions>
@@ -217,14 +217,14 @@
217217
</ui:ContentCard>
218218
<!--跳过-->
219219
<ui:ContentCard Padding="20" Margin="0" Background="#fef5f4">
220-
221-
<Grid >
220+
221+
<Grid >
222222
<Grid.ColumnDefinitions>
223223
<ColumnDefinition Width="*"/>
224224
<ColumnDefinition Width="*"/>
225225

226226
</Grid.ColumnDefinitions>
227-
227+
228228
<Grid Width="350" HorizontalAlignment="Left" >
229229
<Grid.RowDefinitions>
230230
<RowDefinition Height="*"/>
@@ -250,8 +250,8 @@
250250
</StackPanel>
251251

252252
</Grid>
253-
</Grid>
254-
253+
</Grid>
254+
255255
</ui:ContentCard>
256256
</StackPanel>
257257
</ScrollViewer>
@@ -272,13 +272,24 @@
272272
<RowDefinition Height="50"/>
273273
<RowDefinition Height="*"/>
274274
</Grid.RowDefinitions>
275-
<Grid Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.ColumnSpan="2" Margin="0,7">
275+
<Grid Grid.Row="0" VerticalAlignment="Center" Grid.ColumnSpan="2" Margin="0,7">
276276
<Grid.ColumnDefinitions>
277277
<ColumnDefinition Width="*"/>
278278
<ColumnDefinition Width="*"/>
279279
</Grid.ColumnDefinitions>
280280
<!--<TextBlock Text="{DynamicResource Lang_Monthly}" FontSize="22" FontWeight="Bold" VerticalAlignment="Center"/>-->
281-
<date:DatePicker SelectedYear="{Binding Data.Year,Mode=TwoWay}" SelectedMonth="{Binding Data.Month,Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" />
281+
<date:DatePicker SelectedYear="{Binding Data.Year,Mode=TwoWay}" SelectedMonth="{Binding Data.Month,Mode=TwoWay}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" />
282+
<ui:Project1UIButton
283+
Command="{Binding exportDataCommand}"
284+
Content="{DynamicResource Lang_ExportMonthData}"
285+
Icon="Export"
286+
Padding="10,6"
287+
Grid.Column="1"
288+
Margin="0,0,10,0"
289+
Width="auto"
290+
HorizontalAlignment="Right"
291+
Height="auto"
292+
Style="{DynamicResource basic}"/>
282293
</Grid>
283294
<ScrollViewer Grid.Row="1" Grid.ColumnSpan="2">
284295
<Grid Margin="18">

0 commit comments

Comments
 (0)