<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer" x:Class="TestCrystalReport.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Viewer:CrystalReportsViewer x:Name="crystalReportsViewer" Margin="10"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
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.Navigation;
using System.Windows.Shapes;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
namespace TestCrystalReport
{
//Crystal Report for Visaul Studio SDK download from http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0_5.exe
//Runtime: CRRuntime_32bit_13_0_5
//http://downloads.businessobjects.com/akdlm/crnetruntime/clickonce/CRRuntime_64bit_13_0_5.msi
//http://downloads.businessobjects.com/akdlm/crnetruntime/clickonce/CRRuntime_32bit_13_0_5.msi
//when vs2010 build setup, this 2 runtime msi file need exists at C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bootstrapper\Packages\Crystal Reports for .NET Framework 4.0
//****importent if the app build with x86 then just need CRRuntime_32bit_13_0_5.msi*******
//and for vs2010 build setup can copy CRRuntime_32bit_13_0_5.msi and rename to CRRuntime_64bit_13_0_5.msi
//****************************************************************************************************************************************************
//can use vs2010 to build the setup package, va2012 just has InstallShield2015LimitedEdition and no microsoft setup.
//build setup with InstallShield refer:https://blog.csdn.net/caiandyong/article/details/50586648
//build setup use vs2010 refer article : https://blog.csdn.net/zhang_yling/article/details/88982585
//note : for this App.config need add below section if app use .NETFramework 4.5:
// <startup useLegacyV2RuntimeActivationPolicy="true">
// <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
// </startup>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
string rptDBFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reports\\mm8report.mdb");
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
string rptTempFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reports\\mm8heatsheet3col.rpt");
showReport(rptTempFile);
//crystalReportsViewer.
// CrystalReport
//CrystalDecisions.CrystalReports.ViewerObjectModel.
// MyCrystalReport1 rpt = new MyCrystalReport1();
// rpt.SetDataSource(new PersonDAL().ListAll());
// crystalReportsViewer1.ViewerCore.ReportSource = rpt;
// CrystalDecisions.CrystalReports.Engine.Database
// MessageBox.Show(""+doc.HasSavedData);
/*-------------test set datasource-----------------------------------------------------------------------
DataTable tb=new DataTable() ;
tb.Columns.Add(new DataColumn ("id",DbType.Int32.GetType()));
tb.Columns.Add(new DataColumn("name",DbType.String.GetType() ));
DataRow r = tb.NewRow();
r[0] = 1;
r[0] = "1333333333";
tb.Rows.Add(r);
tb.BeginLoadData();
doc.SetDataSource(tb);
----------------------------------------------------------------------------------------------------------- */
/*-----------------------------test----------------------------------------------------------------------------
for (int i = 0; i < doc.Database.Tables.Count; i++)
{
//doc.Database.Tables[i].Location = aliasName;
//MessageBox.Show(doc.Database.Tables[i].Location);
//doc.Database.Tables[i].SetDataSource(tb);
}
--------------------------------------------------------------------------------------------------------------*/
// MessageBox.Show(doc.DataSourceConnections[0].DatabaseName);
/*tes
using (AdventureWorksEntities db = new AdventureWorksEntities())
{
report.SetDataSource(from c in db.Contacts
select new
{
c.FirstName,
c.LastName,
c.EmailAddress,
c.Phone
});
}
crystalReportsViewer1.ViewerCore.ReportSource = report;
*
*
' print dailog
PrintPreview pp = new PrintPreview();
pp.ShowDialog();
*/
}
void showReport(string rptTempFile) {
try
{
ReportDocument doc = new ReportDocument();
doc.Load(rptTempFile);
doc.DataSourceConnections[0].SetConnection(rptDBFile, "", "", "");
crystalReportsViewer.ViewerCore.ReportSource = doc;
crystalReportsViewer.ShowToggleSidePanelButton = false;
}
catch(Exception ex) {
MessageBox.Show( ex.Message );
}
}
}
}
以上是调用水晶报表的例子
下面是关于打包的问题,在身边注释里有写。
重要的一点是就是下载2个水晶报表的运行时 Runtime: CRRuntime_32bit_13_0_5,这个运行时如同.net framework的性质。
下载地址:http://downloads.businessobjects.com/akdlm/crnetruntime/clickonce/CRRuntime_64bit_13_0_5.msi
http://downloads.businessobjects.com/akdlm/crnetruntime/clickonce/CRRuntime_32bit_13_0_5.msi
版本号要和水晶报表开发包SDK的一致(Crystal Report for Visaul Studio SDK download from http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0_5.exe);
参考: https://blog.csdn.net/zhang_yling/article/details/88982585
VS2017 VS2019 SETUP 参考:https://www.cnblogs.com/lxhbky/p/10695527.html