在WPF应用中使用FastReport.WPF报表模块
FastReport是一个非常不错的报表组件,在Winform应用中常常使用它进行报表的设计、预览展现、打印或者导出文件(PDF、Excel)等,可以设计打印各种各样的报表,本篇随笔继续介绍当前最新的FastReport报表模块,其中FastReport.WPF是专门针对WPF的报表组件。1、回顾FastReport应用
我们回顾一下,我之前在随笔中总结的一些FastReport应用案例,虽然WPF使用上有所差异,不过报表的格式是同样的,处理规则也是大同小异。
《使用FastReport报表工具生成标签打印文档》、《使用FastReport报表工具生成图片格式文档》、《使用FastReport报表工具生成报表PDF文档》、《使用FastReport报表工具实现信封套打功能》、《在Winform开发中使用FastReport创建报表》、《在Vue&Element前端项目中,使用FastReport + pdf.js生成并展示自定义报表》、《利用FastReport传递图片参数,在报表上展示签名信息》,可以说在Winform报表处理中,我还是比较喜欢FastReport的的纯.net开发的报表组件。
我们可以看到,FastReport报表还是非常强大的,可以处理各种不同的报表展示。
2、FastReport.WPF报表模块的使用
FastReport.WPF 报表模块是2023年底才出来的一款专门针对WPF的报表应用,我们在Nugget上搜索FastReport.WPF,添加对应的FastReport.WPF.Demo模块就可以在我们的WPF应用中添加测试版本的FastReport.WPF组件了,如果我们有正式版本,也可以安装后直接添加本地引用文件即可。
报表的预览和设计可以通过直接在WPF后端代码打开对应的报表组件实现相应的效果,也可以通过在页面中展示对应组件(预览组件、设计组件)来承载报表信息,我们针对两种情况分别进行相关的说明。
我们在WPF的应用端中添加一个报表的测试页面,用来测试FastReport的报表处理,如下界面所示。
一般的FastReport报表的处理,差不多就是那几个步骤:
1、创建报表对象
2、加载报表文件
3、加载报表相关数据或者参数
4、调用报表设计或者预览处理。
我们来看看简单的按钮单击调用报表的处理,如下代码所示。
/// <summary>
/// 报表预览
/// </summary>
private async void btnPreviewReport_Click(object sender, RoutedEventArgs e)
{
var reportFile = Path.Combine(baseDir, "Report/Simple List.frx");
using (var report = new Report())
{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//加载报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.Load(reportFile);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//创建数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>var ds = GetNorthWindDataSet();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//绑定数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.RegisterData(ds, "NorthWind");
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//运行报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.Show();
}
}
/// <summary>
/// 报表设计
/// </summary>
private void btnDesignReport_Click(object sender, RoutedEventArgs e)
{
var reportFile = Path.Combine(baseDir, "Report/Simple List.frx");
using (var report = new Report())
{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//加载报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.Load(reportFile);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//创建数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>var ds = GetNorthWindDataSet();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//绑定数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.RegisterData(ds, "NorthWind");
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//运行报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.Design();
}
}当然我们也可以在Xaml中定义Command来处理对应的按钮事件。
Xam界面代码如下所示:
<ui:CardAction
Grid.Row="0"
Grid.Column="0"
Margin="0,0,14,0"
Padding="0"
VerticalAlignment="Stretch"
Command="{Binding DataContext.PreviewReportCommand, Mode=OneWay}"
CommandParameter="Simple List.frx"
IsChevronVisible="False">
<Grid>
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><Grid.ColumnDefinitions>
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> <ColumnDefinition Width="Auto" />
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> <ColumnDefinition Width="*" />
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel></Grid.ColumnDefinitions>
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><Image
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Width="60"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Margin="24,0,0,0"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Source="/Assets/dashboardItem1.png" />
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Grid.Column="1"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Margin="24"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> VerticalAlignment="Center">
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> <ui:TextBlock
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>FontTypography="BodyStrong"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>Text="报表打印预览"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>TextWrapping="WrapWithOverflow" />
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> <ui:TextBlock
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>Appearance="Tertiary"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>Text="NorthWind报表预览"
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>TextWrapping="WrapWithOverflow" />
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel></StackPanel>
</Grid>
</ui:CardAction>Command的处理后端代码如下所示。
/// <summary>
/// 预览报表
/// </summary>
/// <param name="path">报表路径</param>
private void PreviewReport(string path)
{
var reportFile = Path.Combine(Environment.CurrentDirectory, $"Report/{path}");
if(File.Exists(reportFile))
{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>using (var report = new Report())
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //加载报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.Load(reportFile);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //创建数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> var ds = GetNorthWindDataSet();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //绑定数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.RegisterData(ds, "NorthWind");
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //运行报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.ShowAsync();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>}
}
}除了我们这里使用MVVM的处理定义,代码逻辑上和单击事件的处理差不多。
FastReport报表可以导出Excel、HTML、PDF等文档格式的文件,如下代码是导出PDF的处理代码,可以把报表导出和预览效果一样的PDF文件。
/// <summary>
/// 导出PDF
/// </summary>
/// <param name="path"></param>
private void ReportExport(string path)
{
var reportFile = Path.Combine(baseDir, $"Report/{path}");
if (File.Exists(reportFile))
{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>using (var report = new Report())
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>{
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //加载报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.Load(reportFile);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //创建数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> var ds = GetNorthWindDataSet();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //绑定数据源
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.RegisterData(ds, "NorthWind");
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //运行报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> report.Prepare();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //导出PDF报表
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> var file = FileDialogHelper.SavePdf("result.pdf");
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> if (!string.IsNullOrEmpty(file))
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> {
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>var export = new<strong> PDFExport</strong>();
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><strong>report.Export</strong>(export, file);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> }
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> //打开PDF
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> if (File.Exists(file))
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> {
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>Process.Start("explorer.exe", file);
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> }
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>}
}
}导出文件后,我们在WPF程序中,可以通过 Process.Start("explorer.exe", file); 命令进行打开PDF文档。
这种通过FastReport内置类的Design或者Show方法展示的报表,是一个独立的窗口打开的。
有时候为了方便,我们可能需要设计一个页面或者窗口来放置我们的设计或者预览报表,那么我们可以通过在页面中展示对应组件(预览组件、设计组件)来承载报表信息。
需要在页面中添加对应的命名空间,如下代码所示。
在页面中可以放置对应的组件对象,如下代码所示。
<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>展示报表的时候,后台代码处理如下所示。
private void btnShowReport_Click(object sender, RoutedEventArgs e){ var reportFile = Path.Combine(baseDir, "Report/report.frx"); var report = new Report();<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//加载报表<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>report.Load(reportFile); //创建数据源 var list = CreateBusinessObject(); //绑定数据源 report.RegisterData(list, "Categories"); report.PrepareAsync(previewControl); //运行报表}private void btnShowDesign_Click(object sender, RoutedEventArgs e){ var reportFile = Path.Combine(baseDir, "Report/report.frx"); var report = new Report(); //加载报表 report.Load(reportFile); //创建数据源 var list = CreateBusinessObject(); //绑定数据源 report.RegisterData(list, "Categories"); designerControl.Report = report;}<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>
当然我们也可以单独设计一个页面来承载,如下页面所示。
通过页面的封装,我们在页面中提供一个公用的报表组件。
/// /// FastReportPreview.xaml 的交互逻辑/// public partial class FastReportPreview : Window{ /// /// 报表对象 /// public Report Report { get; set; } = new();//初始化对象 public FastReportPreview() {<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>InitializeComponent();<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>this.previewControl.Loaded += (s, e) =><StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>{<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> Report.PrepareAsync(previewControl);<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel> }; }}在调用报表页面展示的时候,对报表对象做相应的处理即可,然后调用窗口进行展示。
/// /// 使用自定义窗口打开报表预览/// private void btnShowReport2_Click(object sender, RoutedEventArgs e){ var dlg = App.GetService(); if (dlg != null) {<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>var reportFile = Path.Combine(baseDir, "Report/report.frx");<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//加载报表<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>dlg.Report.Load(reportFile);<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//创建数据源<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>var list = CreateBusinessObject();<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>//绑定数据源<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>dlg.Report.RegisterData(list, "Categories");<StackPanel
x:Name="reportPanel"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="10">
<<strong>Preview:WpfPreviewControl
</strong>x:Name="previewControl"
Height="600"
Margin="10"
ToolbarVisible="False" />
<<strong>Design:WpfDesignerControl
</strong>x:Name="designerControl"
Height="600"
Margin="10" />
</StackPanel>dlg.ShowDialog(); }}同理设计报表也是一样的处理方式,不在赘述。
以上就是我WPF应用中使用FastReport.WPF报表模块的一些经验总结,结合之前的Winform案例经验,我们可以充分的利用FastReport的报表的相关功能,实现我们所需要的报表展示或者文件导出了。
来源:https://www.cnblogs.com/wuhuacong/p/17969163
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]