1

私は、特定のレポートを毎日作成し、リスト内の多数の人に電子メールで送信できるようにしようとしています。

Hangfire を定期的なジョブでテストしましたが、うまく機能します。それは問題ではありません。しかし、既存の Crystal Report ファイル (.rpt) からレポートを作成しようとしています。基本的に、このジョブが実行されると、コードがレポートを作成し、指定されたパスのディスクに PDF として保存し、それを添付ファイルとして電子メールで送信できるようにしたいと考えています。そのため、Web ページでレポートを表示できる必要はありません。コード ビハインドでレポートを生成し、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、Version=0.0.0.0、Culture=neutral、PublicKeyToken=null' またはその依存関係の 1 つを読み込めませんでした。システムは、指定されたファイルを見つけることができません。

System.IO.FileNotFoundException: ファイルまたはアセンブリ 'App_global.asax.twm32qri, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' またはその依存関係の 1 つを読み込めませんでした。システムは、指定されたファイルを見つけることができません。ファイル名: 'App_global.asax.twm32qri, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean System.RuntimeTypeHandle.GetTypeByName(文字列名、ブール型 throwOnError、ブール型 ignoreCase、ブール型 ReflectionOnly、StackCrawlMark& stackMark、IntPtr pPrivHostBinder、ブール型 loadTypeFromPartialName) で System.Type.GetType(文字列型名、

WRN: アセンブリ バインディングのログがオフになっています。アセンブリ バインド エラーのログを有効にするには、レジストリ値 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) を 1 に設定します。この機能をオフにするには、レジストリ値 [HKLM\Software\Microsoft\Fusion!EnableLog] を削除します。

編集:

私も取得している別のエラーがあります。これは、レポート ファイルの読み込みと関係があります。

Failed ジョブの実行中に例外が発生しました。CrystalDecisions.CrystalReports.Engine.LoadSaveReportException

レポート ファイルのパスが無効です。

CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: レポート ファイルのパスが無効です。CrystalDecisions.CrystalReports.Engine.ExceptionThrower.ThrowEngineException(String messageID, EngineExceptionErrorID id) で CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(String, OpenReportMethod openMethod filename, Int16 parentJob) で CrystalDecisions.CrystalReports.Engine.ReportDocument.EnsureLoadReport() でCrystalDecisions.CrystalReports.Engine.ReportDocument.SetDatabaseLogon(String user, String password)パス\Global.asax.cs:line 98の Intranet.Global.GenerateReport() で

4

1 に答える 1

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);
    }
}
于 2015-06-18T21:41:39.687 に答える