CRYSTAL REPORTのWPFでの使い方
クリスタルレポートのWPFを使用するには、以下のステップに従って下さい。
- あなたの WPF プロジェクトを Visual Studio で開きます。
- 「ソリューション エクスプローラー」でプロジェクト名を右クリック、「NuGet パッケージの管理」を選択します。
- NuGetパッケージマネージャーでCrystal Reportsを検索し、適切なバージョンをインストールします。
- WPFウィンドウにボタンやその他のイベントのトリガーとなるコントロールを追加する。
- ボタンのClickイベントハンドラーでCrystal Reportsを起動するコードを作成します。
以下に例を示します。
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
private void Button_Click(object sender, RoutedEventArgs e)
{
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load("YourReportFile.rpt"); // 替换为您的报表文件路径
// 设置报表参数(如果有)
ParameterField parameterField = new ParameterField();
parameterField.Name = "ParameterName";
ParameterDiscreteValue parameterValue = new ParameterDiscreteValue();
parameterValue.Value = "ParameterValue";
parameterField.CurrentValues.Add(parameterValue);
reportDocument.DataDefinition.ParameterFields.Add(parameterField);
// 设置数据源
reportDocument.SetDataSource(yourDataSource); // 替换为您的数据源
// 在Crystal Reports Viewer中显示报表
CrystalReportsViewer crystalReportsViewer = new CrystalReportsViewer();
crystalReportsViewer.ReportSource = reportDocument;
crystalReportsViewer.Show();
}
実際のレポートファイルパスに「YourReportFile.rpt」を置換して、必要に応じてレポートパラメータとデータソースを設定してください。
Crystal ReportsをWPFで使用する場合、使用しているCrystal Reportsのバージョンによって動作方法が異なる場合がありますので、ご使用のバージョンに対応したドキュメントやサンプルに準拠するようにしてください。