如何在 wpf 中显示 crystal 报表查看器的打印对话框



在我的打印窗口中,我编写了以下代码:

public void ShowReport(ReportDocument rp)
    {
        MyCrystalReportsViewer.ViewerCore.ReportSource = rp;
        this.WindowState = WindowState.Maximized;
        MyCrystalReportsViewer.ViewerCore.PrintReport();
    }

我从另一个类调用此方法,但是当我显示打印窗口时,默认情况下没有显示打印对话框。

任何人都可以

解决我的问题以在我调用打印窗口时显示打印对话框。

提前致谢

也许这会有所帮助。 我只是使用报表查看器中提供的打印机制,而不是尝试创建自己的打印对话框。此外,我发现Crystal的WPF实现很差,缺少我需要的许多功能,因此我将WinForm版本托管在WindowsFormsHost中。 请尝试这条路线,因为我说过WPF版本非常差,Crystal网站上的在线支持非常糟糕。 这是我的代码-

XAML中我托管WindowsFormHost(crystal的较新的WPF版本遗漏了许多我需要的选项):

<Window x:Class="Example.Views.Reports.ReportView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterOwner"
        xmlns:Viewer="clr-namespace:CrystalDecisions.Windows.Forms;assembly=CrystalDecisions.Windows.Forms"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="{Binding WindowTitle}" >
    <!--xmlns:Viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"-->
    <Grid>
        <!--<Viewer:CrystalReportsViewer Name="ReportViewer"/>-->
        <WindowsFormsHost>
            <Viewer:CrystalReportViewer x:Name="ReportViewer"/>
        </WindowsFormsHost>
    </Grid>
</Window>

使其工作的代码隐藏:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.IO;
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 Bill.Constants;
using Bill.Models.Reports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
namespace Example.Views.Reports
{
    /// <summary>
    /// Interaction logic for ReportView.xaml
    /// </summary>
    public partial class ReportView : Window, INotifyPropertyChanged
    {
        public ReportView(Report report)
        {
            InitializeComponent();
            //WPF - not needed
            //this.ReportViewer.Owner = Window.GetWindow(this);  //added to fix null parameter window bug in Crystal report 
            if (Application.Current.MainWindow.IsLoaded)
            {
                this.Owner = Application.Current.MainWindow;
            }
            WindowTitle = report.DisplayName;
            this.DataContext = this;
            ShowReport(report.FileInfo.FullName);
        }
        private string _windowTitle;
        /// <summary>
        /// Name of the report, shown in Window Title
        /// </summary>
        public string WindowTitle
        {
            get { return _windowTitle; }
            set
            {
                if (_windowTitle != value)
                {
                    _windowTitle = value;
                    FirePropertyChanged("WindowTitle");
                }
            }
        }
        /// <summary>
        /// Show the selected report
        /// </summary>
        /// <param name="reportPath"></param>
        private void ShowReport(string reportPath)
        {
            ReportDocument report = new ReportDocument();
            if (!String.IsNullOrEmpty(reportPath))
            {
                TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
                TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
                ConnectionInfo crConnectionInfo = new ConnectionInfo();
                Tables CrTables;
                report.Load(reportPath);
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionStrings.CurrentConnection.ConnectionString);  //from config.xml
                crConnectionInfo.ServerName = builder.DataSource;
                crConnectionInfo.DatabaseName = builder.InitialCatalog;
                crConnectionInfo.IntegratedSecurity = true;
                CrTables = report.Database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
                {
                    crtableLogoninfo = CrTable.LogOnInfo;
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                    CrTable.ApplyLogOnInfo(crtableLogoninfo);
                }
//WinForms values
                ReportViewer.ReportSource = report;
                ReportViewer.EnableDrillDown = false;
                //ReportViewer.AllowedExportFormats=ExportFormatType.
                ReportViewer.ShowLogo = false;
                ReportViewer.ShowRefreshButton = false;
                ReportViewer.ShowParameterPanelButton = false;
                ReportViewer.ShowGroupTreeButton = false;
                ReportViewer.UseWaitCursor = false;
                ReportViewer.ToolPanelView = ToolPanelViewType.None;
                //report.Refresh();
            }
        }
        #region INotifyPropertyChange
        public event PropertyChangedEventHandler PropertyChanged;
        protected void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}

我以两种方式做到了,使用窗口窗体,我从页面调用到窗口窗体,在窗口窗体中我嵌入了 CrystalReportViewer,窗口窗体加载了 crystal 报告,这是我做过的最简单、最实用的方法,代码:

Dim rpt As New rpt_Crystal_report
Dim frm As New windowform
Frm.CrystalReportViewer1.ReportSource = rpt
Frm.Refresh ()
Frm.ShowDialog ()
Frm.Dispose ()

页面中加载水晶报告的另一种方法是执行以下操作:例如,我有第 1 页和第 2 页,在第 1 页内我有一个打印按钮,其中包含以下代码, 将 p1 调暗为新页面2 导航服务导航 (p1)

在第 2 页中,我嵌入了与在窗口窗体中相同的水晶报表查看器,但在第 2 页的加载中,我有以下代码, 调暗为新rpt_Crystal_report Me.CrystalReportsViewer1.ViewerCore.ReportSource = rpt

通过这种方式,我设法在页面上显示报告,

如果有人有更好的想法并且有效,那就太好了,问候!

最新更新