【问题标题】:Hangfire + Crystal Reports email?Hangfire + Crystal Reports 电子邮件?
【发布时间】:2015-06-17 15:50:08
【问题描述】:

我正在努力做到这一点,以便我们可以每天生成某些报告并将它们通过电子邮件发送给列表中的一群人。

我已经测试了 Hangfire 的重复性工作,它运行良好。所以这不是问题。但我正在尝试从我现有的 Crystal Report 文件 (.rpt) 创建一个报告。基本上我想这样做,以便在执行此作业时,代码将创建报告,将其以 PDF 格式保存到指定路径的磁盘中,然后我可以将其作为附件通过电子邮件发送给人们。因此,无需能够在网页上查看报告。这个想法实际上是在后面的代码中生成报告,将其保存为 PDF,并在保存后从后面的代码中通过电子邮件发送。

我遇到的问题与水晶报告的实际生成和保存有关。 顺便说一句,我在测试中生成了一个 excel 文件,但我会将其更改为 PDF 以获取实际报告。这是我迄今为止生成报告的内容:

        string path = @"Save folder relative-path";
        //"report" is declared at the class level and instantiated below.
        report = new ReportDocument();
        report.SetDatabaseLogon(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
        report.Load(Server.MapPath("Relative path to the report"));
        report.SetDataSource(GetDataSet()); //This gets the dataset filled with data for the report

        try
        {
            ExportOptions options = new ExportOptions();

            DiskFileDestinationOptions diskFileOptions = new DiskFileDestinationOptions();
            ExcelFormatOptions excelOptions = new ExcelFormatOptions();
            diskFileOptions.DiskFileName = path + "Test Report.xls";

            options.ExportDestinationType = ExportDestinationType.DiskFile;
            options.ExportFormatType = ExportFormatType.Excel;
            options.ExportDestinationOptions = diskFileOptions;
            options.ExportFormatOptions = excelOptions;
            report.Export();

            /*
            This is where I would call a method to email the report to people
            */
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error generating report: " + ex.Message);
        }

此代码位于 Web 应用程序的 global.asax 文件中的 Application_Start 处调用的方法中。当我运行应用程序时,当我在 Hangfire 仪表板中查看失败的作业时,作业失败并引发此错误,即使我知道我的代码中的路径是正确的:

System.IO.FileNotFoundException 无法加载文件或程序集 'App_global.asax.twm32qri,版本=0.0.0.0,文化=中性, PublicKeyToken=null' 或其依赖项之一。系统无法 找到指定的文件。

System.IO.FileNotFoundException:无法加载文件或程序集 'App_global.asax.twm32qri,版本=0.0.0.0,文化=中性, PublicKeyToken=null' 或其依赖项之一。系统无法 找到指定的文件。文件名:'App_global.asax.twm32qri, 版本=0.0.0.0,文化=中性,PublicKeyToken=null' 在 System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔值 throwOnError, Boolean ignoreCase, Boolean reflectOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName,ObjectHandleOnStack 类型)在 System.RuntimeTypeHandle.GetTypeByName(字符串名称,布尔值 throwOnError, Boolean ignoreCase, Boolean reflectOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) 在 System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) 在 Hangfire.Storage.InvocationData.Deserialize()

警告:程序集绑定日志记录已关闭。启用程序集绑定 失败记录,设置注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 为 1。注意:有 是与装配绑定失败相关的一些性能损失 记录。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

编辑:

我也遇到了另一个错误。这个有事 加载报告文件。

失败 在执行作业期间发生异常。 CrystalDecisions.CrystalReports.Engine.LoadSaveReportException

报告文件路径无效。

CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: 报告文件路径无效。在 CrystalDecisions.CrystalReports.Engine.ExceptionThrower.ThrowEngineException(字符串 messageID, EngineExceptionErrorID id) 在 CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(字符串 文件名,OpenReportMethod openMethod,Int16 parentJob)在 CrystalDecisions.CrystalReports.Engine.ReportDocument.EnsureLoadReport() 在 CrystalDecisions.CrystalReports.Engine.ReportDocument.SetDatabaseLogon(字符串 用户,字符串密码)在 Intranet.Global.GenerateReport() 中 路径\Global.asax.cs:第 98 行

【问题讨论】:

  • 该异常不是来自 Crystal...不知道 hangfire 是什么或为什么需要在 Web 应用程序中编写它,但显然它没有加载 global.asax 的程序集
  • @dotjoe Hangfire 用于创建后台作业,这些作业本质上可以是即发即弃、延迟或重复。我会重复出现,因为我每天都会通过电子邮件发送报告。我知道错误不是来自水晶。但我不确定如何生成报告并将其保存到磁盘。
  • 导出到磁盘时遇到什么错误?目标目录的写权限好吗?
  • @dotjoe 我在问题中包含了错误。这是一个 System.IO.FileNotFoundException。写权限肯定到位。
  • 是的,但是在您到达 Crystal 代码之前,该异常就被触发了,对吧??

标签: c# asp.net .net crystal-reports hangfire


【解决方案1】:

解决了这个问题。我显然需要使用 CrystalReportViewer 对象并将 ReportDocument 对象设置为其源。 CrystalReportViewer 类位于 CrystalDecisions.Web 命名空间中。

using (ReportDocument report = new ReportDocument())
{
    using (CrystalReportViewer viewer = new CrystalReportViewer())
    {
        string path = System.Web.Hosting.HostingEnvironment.MapPath(@"Destination path here");
        report.Load(System.Web.Hosting.HostingEnvironment.MapPath(@"Path to .rpt file here"));
        report.SetDatabaseLogon(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);

        string file = path + "TestReport.xls";

        //These two lines below are important. The report won't generate without them.
        viewer.ReportSource = report;
        viewer.RefreshReport();

        //Just deleting the file if it exists.
        if (File.Exists(file))
            File.Delete(file);

        report.ExportToDisk(ExportFormatType.Excel, diskFileOptions.DiskFileName);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多