首页 文章

在c#中直接将Crystal Report运行到PDF

提问于
浏览
0

我有一个Crystal Report,我试图通过一个使用报表查看器的网站运行,并且(使用相同的.rpt)通过后台进程运行,该进程可以使用导出选项直接生成报告到流或文件 .

我没有使用数据库,但创建了一个xml架构文件,并生成一个xml数据文件,以根据唯一的用户数据加载到报表中 .

当我创建报告时,我创建了一个ADO.NET(XML)连接来设计它并将其指向我的模式文件(所有这些都在VS 2012中) .

在运行时,我使用.net DataSet对象并使用ReadXmlSchema和ReadXml方法获取我的报告数据,然后只设置我的ReportDocument对象的数据源 .

这在我的Web应用程序中使用报表查看器都很有效 . 这是工作代码:

ReportDocument report = new ReportDocument();
    report.Load(reportPath);

    DataSet reportData = new DataSet();

    reportData.ReadXmlSchema("MySchema.xml");
    reportData.ReadXml("SampleData1.xml");

    report.SetDataSource(reportData);

    CrystalReportViewer1.ReportSource = report;

我的问题/问题是如何在没有报表查看器的情况下以编程方式运行它并让它生成PDF?基本上与上面相同的代码减去报表查看器部分并添加一个导出选项 . 我有一个长期运行的后台流程,并希望将此报告作为PDF附加到我生成的电子邮件中 .

我可以使用与Web版本相同的方法,但是当我到达SetDataSource行并尝试将其设置为我的DataSet时,我收到并且错误地说“未知数据库连接错误...”与不使用SetDatabaseLogon方法 .

我试图了解当我没有连接到数据库时,Crystal Reports对SetDatabaseLogon方法的期望 . 如果它假定.RPT文件中的原始ADO.NET(XML)连接是合法的数据库连接,那么登录参数是什么?这只是一个档案 . 没有用户名,密码等 .

有没有更好的方法可以在没有Report Viewer的情况下从现有Crystal Report直接进行PDF格式化?我已经查看了各种链接,但没有发现任何不涉及某种连接字符串的内容 . 如果我必须使用SetDatabaseLogin,那么仅使用XML文件时哪些值会起作用?

感谢您提供任何信息或链接!

编辑:在查看“未知数据库连接器错误”(不是我之前说过的连接)时,我看到它实际上是在我的Temp文件夹中查看.RPT而不是在我认为我正在加载它的远程位置 . 也许这就是问题?

3 回答

  • 0

    试试这段代码:

    report.ExportOptions.DestinationType = CRExportDestinationType.crEDTDiskFile;
                    report.ExportOptions.FormatType = CRExportFormatType.crEFTPortableDocFormat;
                    DiskFileDestinationOptions fileOption = new DiskFileDestinationOptions();
                    fileOption.DiskFileName = "Test.pdf";
                    report.ExportOptions.DiskFileName = fileOption.DiskFileName;
                    report.Export(false);
    
  • 0

    你尝试过这样的事情:

    Report.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("Foldername/Reportname.pdf"));
    
  • 0

    你可以试试这个:

    PageMargins margins = new PageMargins { topMargin = 100, leftMargin = 250, bottomMargin = 100, rightMargin = 100 };
    report.PrintOptions.ApplyPageMargins(margins );
    Stream str = report.ExportToStream(ExportFormatType.PortableDocFormat);
    int length = Convert.ToInt32(str.Length);
    byte[] bytes = new byte[length];
    str.Read(bytes, 0, length);
    str.Close();
    File.WriteAllBytes(@"C:\Report.pdf", bytes);
    

    我希望它有所帮助 .

相关问题