从 C# 控制台应用程序启动 Crystal 报告

Launch a Crystal Report from a C# console application

我用 C# 使用 CrystalReportForm 编写了一份报告。我将其编写为 Windows 表单应用程序。 (Visual Studio 2015)。它运行良好,但我需要将其转换为控制台应用程序,以便我可以将其自动化。

我使用了 CrystalReportViewer,这可能不是最好的方法。任何 assistance/direction 表示赞赏。

DSP = new Classes.DSP_Object(txtStartDate.Text);
var crf = new CrystalReportForm();
var dsp = new CrystalReports.DSP_Report();
((TextObject)dsp.ReportDefinition.Sections["Section1"].ReportObjects
["lblWeekOfHeader"]).Text = DSP.lblWeekOfHeader;
//Lots more assignments of object fields to report
    .
    .
    .
crf.crv1.ReportSource = dsp;
crf.Show();
//The report looks great at this point.         

由于您需要从控制台应用程序 运行 它,我不会尝试在控制台应用程序中托管查看器 - 那是行不通的。

相反,您可以 运行 来自控制台应用程序的报告,另存为 pdf 并显示 pdf。

生成报告后,您可以执行以下操作:

Process p=new Process();
p.StartInfo.FileName = @"G:\GeneratedCrystalReport.pdf";
p.Start();

这个问题表明我对Crystal 和C# 的无知。这是一个简单的修复。只需加载文档而不是通过查看器引用它。