Skip to content

Commit

Permalink
添加月份数据导出Excel功能
Browse files Browse the repository at this point in the history
  • Loading branch information
noberumotto committed Oct 17, 2021
1 parent 738a99a commit 0fc3c6c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/Local/ProjectEye/ProjectEye.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
</PackageReference>
<PackageReference Include="Npoi.Mapper">
<Version>3.5.1</Version>
</PackageReference>
<PackageReference Include="SQLite.CodeFirst">
<Version>1.5.3.29</Version>
</PackageReference>
Expand Down
1 change: 1 addition & 0 deletions src/Local/ProjectEye/Resources/Language/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,6 @@
<sys:String x:Key="Lang_DataWindowRestTimeImagePath">Rest time placeholder image</sys:String>
<sys:String x:Key="Lang_DataWindowSkipImagePath">Skip placeholder image</sys:String>
<sys:String x:Key="Lang_StatisticsWindow">Statistics window</sys:String>
<sys:String x:Key="Lang_ExportMonthData">Export month data</sys:String>

</ResourceDictionary>
1 change: 1 addition & 0 deletions src/Local/ProjectEye/Resources/Language/zh.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,6 @@
<sys:String x:Key="Lang_DataWindowRestTimeImagePath">休息时间占位图片</sys:String>
<sys:String x:Key="Lang_DataWindowSkipImagePath">跳过次数占位图片</sys:String>
<sys:String x:Key="Lang_StatisticsWindow">数据统计窗口</sys:String>
<sys:String x:Key="Lang_ExportMonthData">导出本月数据</sys:String>

</ResourceDictionary>
47 changes: 45 additions & 2 deletions src/Local/ProjectEye/ViewModels/StatisticViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Project1.UI.Controls.ChartControl.Models;
using Npoi.Mapper;
using Project1.UI.Controls.ChartControl.Models;
using Project1.UI.Cores;
using ProjectEye.Core;
using ProjectEye.Core.Service;
Expand Down Expand Up @@ -26,6 +27,8 @@ public class StatisticViewModel

public Command CloseOnboardingCommand { get; set; }
public Command GenerateMonthlyDataImgCommand { get; set; }
public Command exportDataCommand { get; set; }


public StatisticViewModel(
StatisticService statistic,
Expand All @@ -40,6 +43,8 @@ public StatisticViewModel(

CloseOnboardingCommand = new Command(new Action<object>(OnCloseOnboardingCommand));
GenerateMonthlyDataImgCommand = new Command(new Action<object>(OnGenerateMonthlyDataImgCommand));
exportDataCommand = new Command(new Action<object>(OnExportDataCommand));

Data = new StatisticModel();
Data.Year = DateTime.Now.Year;
Data.Month = DateTime.Now.Month;
Expand All @@ -65,14 +70,16 @@ public StatisticViewModel(
LoadImages();
}



private void LoadImages()
{
string worktimeimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowWorkTimeImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/web_developer.png" : config.options.Style.DataWindowWorkTimeImagePath;
string resttimeimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowRestTimeImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/coffee_lover.png" : config.options.Style.DataWindowRestTimeImagePath;
string skipimgpath = string.IsNullOrEmpty(config.options.Style.DataWindowSkipImagePath) ? "pack://application:,,,/ProjectEye;component/Resources/office_work_.png" : config.options.Style.DataWindowSkipImagePath;

Data.WorktimeImageSource = BitmapImager.Load(worktimeimgpath);
Data.ResttimeImageSource= BitmapImager.Load(resttimeimgpath);
Data.ResttimeImageSource = BitmapImager.Load(resttimeimgpath);
Data.SkipImageSource = BitmapImager.Load(skipimgpath);

}
Expand Down Expand Up @@ -429,5 +436,41 @@ private void OnGenerateMonthlyDataImgCommand(object obj)
Data.MonthWork).Generate();
}
}
public struct XlsxData
{

}
private void OnExportDataCommand(object obj)
{
try
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Project Eye statistic data " + Data.Year + Data.Month;
dlg.DefaultExt = ".xlsx";
dlg.Filter = "(.xlsx)|*.xlsx";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// 获取选择月份的数据
var monthData = statistic.GetData(Data.Year, Data.Month);

// 重新构建友好结构

var mapper = new Mapper();
mapper
.Map<Core.Models.Statistic.StatisticModel>("Date 日期", o => o.Date)
.Map<Core.Models.Statistic.StatisticModel>("Work(hours) 工作(小时)", o => o.WorkingTime)
.Map<Core.Models.Statistic.StatisticModel>("Rest(minutes) 休息(分钟)", o => o.ResetTime)
.Map<Core.Models.Statistic.StatisticModel>("Skip 跳过(次)", o => o.SkipCount)
.Ignore<Core.Models.Statistic.StatisticModel>(o => o.ID)
.Save(dlg.FileName, monthData, $"{Data.Year}{Data.Month}", overwrite: true);
}
}
catch (Exception e)
{
LogHelper.Error(e.Message);
MessageBox.Show("导出数据失败,了解详情请查看错误日志");
}
}
}
}
29 changes: 20 additions & 9 deletions src/Local/ProjectEye/Views/StatisticWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />-->
</Grid>

</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<DropShadowEffect x:Key="CardShadow" BlurRadius="12" Direction="0" ShadowDepth="0" Opacity=".3" RenderingBias="Performance" ></DropShadowEffect>

</Style.Resources>

<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
Expand Down Expand Up @@ -217,14 +217,14 @@
</ui:ContentCard>
<!--跳过-->
<ui:ContentCard Padding="20" Margin="0" Background="#fef5f4">
<Grid >

<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Grid Width="350" HorizontalAlignment="Left" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
Expand All @@ -250,8 +250,8 @@
</StackPanel>

</Grid>
</Grid>
</Grid>

</ui:ContentCard>
</StackPanel>
</ScrollViewer>
Expand All @@ -272,13 +272,24 @@
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.ColumnSpan="2" Margin="0,7">
<Grid Grid.Row="0" VerticalAlignment="Center" Grid.ColumnSpan="2" Margin="0,7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--<TextBlock Text="{DynamicResource Lang_Monthly}" FontSize="22" FontWeight="Bold" VerticalAlignment="Center"/>-->
<date:DatePicker SelectedYear="{Binding Data.Year,Mode=TwoWay}" SelectedMonth="{Binding Data.Month,Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" />
<date:DatePicker SelectedYear="{Binding Data.Year,Mode=TwoWay}" SelectedMonth="{Binding Data.Month,Mode=TwoWay}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" />
<ui:Project1UIButton
Command="{Binding exportDataCommand}"
Content="{DynamicResource Lang_ExportMonthData}"
Icon="Export"
Padding="10,6"
Grid.Column="1"
Margin="0,0,10,0"
Width="auto"
HorizontalAlignment="Right"
Height="auto"
Style="{DynamicResource basic}"/>
</Grid>
<ScrollViewer Grid.Row="1" Grid.ColumnSpan="2">
<Grid Margin="18">
Expand Down

0 comments on commit 0fc3c6c

Please sign in to comment.