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

时间:2015-03-16 10:35:36

标签: c# wpf printing visual-studio-2013 crystal-reports

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

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

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

当我打电话给打印窗口时,任何人都可以解决我的问题以显示打印对话框。

提前致谢

2 个答案:

答案 0 :(得分:1)

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

我主持WindowsFormHost的XAML(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
    }
}

答案 1 :(得分:0)

我用两种方式做到了,使用窗口形式,我从页面调用窗口窗体,在窗口窗体内嵌入CrystalReportViewer,窗口窗体加载水晶报告,这是最简单,最实用的方法已经,代码:

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页内部我有一个打印按钮,其中包含以下代码,     Dim p1 As New page2     NavigationService.Navigate(p1)

在第2页中,我将crystalreportviewer与我在windowform中所做的相同,但在第2页的加载中,我有以下代码,     Dim rpt As New rpt_Crystal_report     Me.CrystalReportsViewer1.ViewerCore.ReportSource = rpt

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

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