2014-09-30 50 views
0

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

此外,我有一个示例WPF应用程序准备从哪里用户将输入jobnumber。我也准备好了水晶报告。用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

我用WPF/C#集成水晶报表,但它应该是非常相似的。确保您安装了开发者版本,以便您可以添加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

+0

如果报告的名称中包含BillingSummary,则不必执行检查的部分,但我将其作为如何将参数传递给报告的示例(因此可以摆脱标准ameter窗口水晶用途等) – 2014-09-30 20:32:19

+0

对不起也忘了我的构造函数中的Report(报告)只是一个包含文件路径的对象,这就是你真正需要的。我的报告对象还包含一个视图名称以及FileInfo。 – 2014-09-30 20:43:30

+0

此ReportViewer窗口将允许报告呈现,导出(即Excel等)并自行打印。 – 2014-09-30 21:14:08