如何在WPF中集成Crystal Reports?

时间:2014-09-30 19:47:11

标签: wpf vb.net crystal-reports

我正在从事WPF(VB)项目,我需要将现有的水晶报告链接到WPF应用程序。用户将从我的WPF应用程序输入作业号码;之后我需要运行查询(从JobHeader中选择*,其中Jobnumber = @ userjobnumber);然后我需要调用水晶报告,该报告将包含该JobNumber的所有详细信息。

此外,我还准备了一个示例WPF应用程序,用户将从中输入作业编号。我也准备好了水晶报告。坚持将水晶报告与WPF集成。有人可以帮帮我吗?长期坚持这个问题并接近截止日期。请帮忙。

由于

这是我的VBCode:

Imports System.Globalization
Imports System.Drawing.Printing
Imports System.Drawing
Imports System.Data
Imports System.Data.SqlClient



Class MainWindow

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    AddHandler printDocument1.PrintPage, AddressOf printDocument1_PrintPage
End Sub

'Declaration  the  global  variables 
Private paperSize As New PaperSize("papersize", 300, 500)
'set the paper size 
Private totalnumber As Integer = 0
'this is for total number of items of the list or array
Private itemperpage As Integer = 0
'this is for no of item per page 
Private printDocument1 As New PrintDocument()
Private printDialog1 As New System.Windows.Forms.PrintDialog()
Private DefaultFont As New Font("Calibri", 20)
Public StrConn As String = "Data Source=it-  12\localAv01;Database=AvTest01;Uid=sa;Pwd=test123"
Dim QueryStr = "Select * from JobHeader where JobNumber="
Dim ConnStr = StrConnAvanti
Dim dTable As DataTable = New DataTable()




Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

    If txtStart.Text.Trim.Length > 0 Then
        itemperpage = 1
        totalnumber = txtStart.Text.Replace("J", "")
        printDialog1.Document = printDocument1
        printDocument1.DefaultPageSettings.PaperSize = paperSize
        printDialog1.ShowDialog()

        'printDocument1.PrinterSettings.PrinterName = "";
        printDocument1.Print()
    Else
        MessageBox.Show("Invalid number")
    End If
End Sub

Private Function CheckNumber(str As String)
    Dim Num As Double
    Return Double.TryParse(str, Num)
End Function

Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)
    Dim currentY As Single = 10

    While totalnumber <= txtStart.Text.Replace("J", "")
        ' check the number of items 

        e.Graphics.DrawString("J" + totalnumber.ToString("000000", CultureInfo.InvariantCulture), DefaultFont, System.Drawing.Brushes.Black, 50, currentY)


        'print each item
        currentY += 20
        ' set a gap between every item
        totalnumber += 1
        'increment count by 1
        If itemperpage < 1 Then
            ' check whether  the number of item(per page) is more than 1 or not
            itemperpage += 1
            ' increment itemperpage by 1
            ' set the HasMorePages property to false , so that no other page will not be added 
            e.HasMorePages = False
        Else

            ' if the number of item(per page) is more than 1 then add one page 
            itemperpage = 1
            'initiate itemperpage to 0 . 
            If totalnumber <= Convert.ToInt32(txtStart.Text.Replace("J", "")) Then
                e.HasMorePages = True
            End If
            'e.HasMorePages raised the PrintPage event once per page .           
            'It will call PrintPage event again
            Return
        End If
    End While
End Sub
End Class

1 个答案:

答案 0 :(得分:2)

我使用WPF / C#来集成Crystal Reports,但它应该非常相似。确保安装了开发人员版本,以便添加Crystal / SAP所需的参考。首先,我制作了一个报告查看器窗口:

<Window x:Class="Bill.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:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
    Title="ReportView" >
<Grid>
    <Viewer:CrystalReportsViewer Name="ReportViewer"/>
</Grid>

接下来是代码隐藏:

using System;
using System.Collections.Generic;
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;

namespace Bill.Views.Reports
{
    /// <summary>
    /// Interaction logic for ReportView.xaml
    /// </summary>
    public partial class ReportView : Window
    {
        public ReportView(Report report)
        {
            InitializeComponent();

            ReportViewer.Owner = Window.GetWindow(this);  //added to fix null parameter window bug in Crytal report 

            if (Application.Current.MainWindow.IsLoaded)
            {
                this.Owner = Application.Current.MainWindow;
            }

            ShowReport(report.FileInfo.FullName);
        }

        /// <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);
                }

                if (reportPath.Contains("BillingSummary"))
                {
                    //report.SetParameterValue("prmBillingCycle", 10);

                    report.SetParameterValue("@BillingTypeID", null);
                    report.SetParameterValue("@BillingModelID", null);

                    report.SetParameterValue("@InvoiceID", null, report.Subreports[1].Name.ToString());
                    report.SetParameterValue("@InvoiceID", null, report.Subreports[2].Name.ToString());

                    report.SetParameterValue("@ForGrid", null, report.Subreports[1].Name.ToString());
                    report.SetParameterValue("@ForGrid", null, report.Subreports[2].Name.ToString());
                }

                ReportViewer.ViewerCore.ReportSource = report;
                ReportViewer.ViewerCore.EnableDrillDown = false;
                ReportViewer.ToggleSidePanel = SAPBusinessObjects.WPF.Viewer.Constants.SidePanelKind.None;
                ReportViewer.ShowLogo = false;
                ReportViewer.ShowToggleSidePanelButton = false;
                ReportViewer.ShowRefreshButton = false;

                //report.Refresh();
            }
        }
    }
}

此外,这是一个帮助我入门的链接:http://scn.sap.com/docs/DOC-54328