【问题标题】:How to display the print dialog box for crystal report viewer in wpf如何在wpf中显示水晶报表查看器的打印对话框
【发布时间】:2015-03-16 10:35:36
【问题描述】:

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

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

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

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

提前致谢

【问题讨论】:

  • 为什么不加载报告(ReportDoucment report= new ReportDocument(); report.Load())然后它有一个打印按钮,会显示打印对话框?
  • 我已经在其他类中加载了报告,然后导航到上述方法。
  • 只有当我像 PrintDialog printDialog = new PrintDialog(); 这样写时,上述语句才有效printDialog.ShowDialog(); MyCrystalReportsViewer.ViewerCore.PrintReport();这里的问题是只有在执行第二条语句时才执行第三条语句。主要问题是出现了两个打印对话框。如果我使用第二条语句,它不会打印当前的 ReportDocument。请任何人帮助我。

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


【解决方案1】:

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

我托管 WindowsFormHost 的 XAML(水晶的较新 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
    }
}

【讨论】:

  • ConnectionStrings.CurrentConnection.ConnectionString 是这样的:Data Source=ServerName;Initial Catalog=My_DB;Integrated Security=True
  • 它对我不起作用。这里的主要问题是 PrintReport() 方法不起作用。
  • 谁能解决我的问题。 drive.google.com/…
  • 我已经给出了上面的示例应用程序链接,
【解决方案2】:

我做了两种方式,使用窗体,从页面调用窗体,窗体内部嵌入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 页内我有一个打印按钮,它具有以下代码, 将 p1 调暗为新页面 2 NavigationService.Navigate (p1)

在第 2 页中,我嵌入了与在 windowform 中相同的 Crystalreportviewer,但在第 2 页的加载中,我有以下代码, 将 rpt 变暗为新的 rpt_Crystal_report Me.CrystalReportsViewer1.ViewerCore.ReportSource = rpt

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多