WPF报表

来源:互联网 发布:怎样消费到淘宝v2 编辑:程序博客网 时间:2024/06/02 02:08

    在机房合作中,在给操作员结账后,为了更方便的看,我们用到了报表,那么wpf的报表如何实现呢,下面就由小编带着一步步走下去


1、添加一个名称为Report的rdlc报表



界面是:

        

现在我们在工具箱拖放一个“表”

 

点下一步

             

继续下一步

           

                    

点击确定,点下一步,下一步

             

点击完成,完成就得到下面界面

                        

                             

绑定完全之后的界面

          

在U层添加引用Microsoft.ReportViewer.WinForms和WindowsFormsIntegration

下面新建一个wpf窗体:MaskLayer.xaml

<span style="font-family:KaiTi_GB2312;font-size:24px;"><UserControl x:Class="JfCooperate.管理员.Report.MaskLayer"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:local="clr-namespace:JfCooperate"             mc:Ignorable="d"              d:DesignHeight="300" d:DesignWidth="300" Opacity="0.85" Background="#fbfcfc">    <Grid>        <TextBlock Text="正在生成报表..." VerticalAlignment="Center" HorizontalAlignment="Center"/>    </Grid></UserControl></span>

其对应的cs代码:

<span style="font-family:KaiTi_GB2312;font-size:24px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace JfCooperate.管理员.Report{    /// <summary>    /// MaskLayer.xaml 的交互逻辑    /// </summary>    public partial class MaskLayer : UserControl    {        public MaskLayer()        {            InitializeComponent();        }    }}</span>

新建wpf窗体:ReportCtrl

ReportCtrl.xaml

<span style="font-family:KaiTi_GB2312;font-size:24px;"><UserControl x:Class="JfCooperate.管理员.Report.ReportCtrl"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"              xmlns:local="clr-namespace:JfCooperate.管理员.Report"             mc:Ignorable="d"              d:DesignHeight="300" d:DesignWidth="300">    <Grid>        <WindowsFormsHost>            <rv:ReportViewer x:Name="ReportViewer"/>        </WindowsFormsHost>        <local:MaskLayer x:Name="maskLayer" Visibility="Collapsed"/>    </Grid></UserControl></span>
其对应的cs代码

<span style="font-family:KaiTi_GB2312;font-size:24px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using Microsoft.Reporting.WinForms;using System.Data;using System.IO;using System.Windows.Navigation;using Package.Facade;using Package.BLL;using Package.Entity;namespace JfCooperate.管理员.Report{    /// <summary>    /// ReportCtrl.xaml 的交互逻辑    /// </summary>    public partial class ReportCtrl : UserControl    {        public ReportCtrl()        {            InitializeComponent();            this.Loaded += ReportCtrl_Loaded;            this.ReportViewer.RenderingComplete += ReportViewer_RenderingComplete;        }        private void ReportCtrl_Loaded(object sender, RoutedEventArgs e)        {            maskLayer.Visibility = Visibility.Visible;            CheckOutEntity ECheck = new CheckOutEntity();            DataTable dt = new DataTable();            ReportFacade fa = new ReportFacade();            dt = fa.QueryReport(ECheck);            if (dt.Rows.Count != 0)            {                ReportDataSource reportDataSource = new ReportDataSource();                //定义数据集名称                reportDataSource.Name = "ReportDataSet";                reportDataSource.Value = dt;                //说明reportViews承载的表名                ReportViewer.LocalReport.ReportPath = Directory.GetCurrentDirectory() + "\\Report.rdlc";                //添加报表数据源                ReportViewer.LocalReport.DataSources.Add(reportDataSource);                ReportViewer.RefreshReport();            }        }        private void ReportViewer_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)        {            maskLayer.Visibility = Visibility.Collapsed;        }    }}</span>

新建ReportWindow.xaml来承载报表

reportwindow.xaml 代码:

<span style="font-family:KaiTi_GB2312;font-size:24px;"><Window x:Class="JfCooperate.管理员.Report.ReportWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:local="clr-namespace:JfCooperate.管理员.Report"        mc:Ignorable="d" WindowStartupLocation="CenterScreen"        Title="报表" Height="279" Width="653.541">    <Grid>        <local:ReportCtrl/>    </Grid></Window></span>

其对应的cs代码:
<span style="font-family:KaiTi_GB2312;font-size:24px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace JfCooperate.管理员.Report{    /// <summary>    /// ReportWindow.xaml 的交互逻辑  Interaction logic for ReportWindow.xaml    /// </summary>    ///     public partial class ReportWindow : Window    {        public ReportWindow()        {            InitializeComponent();        }    }}</span>


   在这里,我想通过查询数据库里面的时间来生成报表,所以我就新建了一个frmReport窗体

                      

xaml代码:

<span style="font-family:KaiTi_GB2312;font-size:24px;"><Controls:MetroWindow x:Class="JfCooperate.管理员.Report.frmReport"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"        Title="账单" Height="300" Width="351.77" Background="#FFFFFF" WindowStartupLocation="CenterScreen">    <Grid>        <DatePicker x:Name="DpStart" HorizontalAlignment="Left" Margin="137,38,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Microsoft YaHei Light"/>        <DatePicker x:Name="DpEnd" HorizontalAlignment="Left" Margin="137,110,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Microsoft YaHei Light" />        <Label Content="起始日期" HorizontalAlignment="Left" Margin="26,38,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Microsoft YaHei Light"/>        <Label Content="终止日期" HorizontalAlignment="Left" Margin="26,108,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Microsoft YaHei Light"/>        <Button x:Name="ShowReport" Content="查询账单" HorizontalAlignment="Left" Margin="97,185,0,0" VerticalAlignment="Top" Width="125" FontSize="24" FontFamily="Microsoft YaHei Light" Click="Button_Click_1"/>        <Label x:Name="lblShow" Content="" HorizontalAlignment="Left" Height="123" Margin="297,38,0,0" VerticalAlignment="Top" Width="45" Foreground="Red"/>    </Grid></Controls:MetroWindow></span>

其cs代码

<span style="font-family:KaiTi_GB2312;font-size:24px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using MahApps.Metro.Controls;using MahApps.Metro.Controls.Dialogs;using Package.Entity;using Package.Facade;namespace JfCooperate.管理员.Report{    /// <summary>    /// frmReport.xaml 的交互逻辑    /// </summary>    public partial class frmReport : MetroWindow    {        public frmReport()        {            InitializeComponent();        }        private void Button_Click_1(object sender, RoutedEventArgs e)        {            if (DpStart.SelectedDate > DpEnd.SelectedDate)            {                lblShow.Content = "终止时间必须大于初始时间";            }            else            {                CheckOutEntity ECheck = new CheckOutEntity();                //先声明后实例化                ECheck.DateStart = (DateTime.Parse(DpStart.Text)).ToString("yyyy-MM-dd");                ECheck.DateEnd = (DateTime.Parse(DpEnd.Text)).ToString("yyyy-MM-dd");            }            ReportWindow rpt = new ReportWindow();            rpt.ShowDialog();        }    }}</span>

结果显示:账单可打印,可导出。

       





0 0