2011-08-06 18 views
9

ASP.NET MVC3 Webアプリケーションを構築しています。Crystalレポートを使用してレポートを作成し、Crystalレポートビューアを使用して表示する必要があります。 ウェブを検索したところ、MVC3 Webアプリケーションでそれを行うための強固な方法が見つかりませんでした。 これを行う方法に関するヒントやアイデアはありますか?MVC3を使用したCrystal Reportsビューア

答えて

4

ハッキングを気にしない場合は、実際にはかなり簡単です。 (CR4VS2010と仮定)

まず、Webフォームページをプロジェクトに追加し、Crystalレポートビューアコントロールを追加します。

CrystalDescisions.CrystalReports.EngineCrystalDescisions.ReportSourceCrystalDescisions.Shared、 とCrystalDescisions.Web

が、それはへの参照を追加したことを確認します。

次に、PageRouteをアプリケーションに追加して、新しく追加されたページにつなぎます。

最後に、これは「BLANK」の最大の痛みでした.CrystalのImage Handlerを動作させる必要があります。多くのはずな方法は、それらのどれもが本当に私のために働いていないので、私は不正行為に頼っ、ネットを中心にして、ここでSOの両方で、あります

public class CrystalImageHandlerController : Controller 
{ 
    // 
    // GET: /CrystalImageHandler.aspx 

    public ActionResult Index() 
    { 
     return Content(""); 
    } 

    protected override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 

     var handler = new CrystalDecisions.Web.CrystalImageHandler(); 
     var app = (HttpApplication)filterContext.RequestContext.HttpContext.GetService(typeof(HttpApplication)); 
     if (app == null) return; 

     handler.ProcessRequest(app.Context); 

    } 
} 

これはどこで、/CrystalReportsImageHandler.aspxとしてこのコントローラへのルートを追加します。 CRはそれがハンドラであることを期待しています。これはエリアでも使用でき、必要に応じてハンドラとページルートを変更するだけです。

あなたの剃刀レイアウトを使用することはできません。視覚的な連続性を得るには、他の手段に頼る必要があります。 (IFrameを使用しました)

+0

ありがとうございます。私は[ここ](http://www.highoncoding.com/Articles/550_Creating_Crystal_Report_in_ASP_NET.aspx)からWebformの部分を取得し、それをあなたのコントローラと組み合わせて、私は自分のレポートを見ることができました。 –

+0

素晴らしい!私はまた、このごちそうにあなたを有効にする必要があります:Webビューアを使用するときは、JS APIがブラウザにあります。ドキュメントは見つかりませんでしたが、iframeを使用して自分のサイトレイアウトで動作させると、サイズ変更の方法が非常に貴重であることがわかりました。オブジェクト名は _UIです。 –

+0

あなたは「このコントローラにルートを/CrystalReportsImageHandler.aspxとして追加する」と言います。どのように私はこれを正確に行うのですか? –

4

私たちのプロジェクトでは、レポートをPDF形式で直接返しています。主に同じプロジェクトでWebFormsとMVCの両方を使用しないことを決めました。なぜなら、主にコードベースをきれいにするためです。

レポートは、手動で作成されたデータと、NHibernateで必要とされるすべての情報を取得するサービスクラスによって埋められたデータで生成されます(ORM永続性とDB抽象化のレイヤーを使用しているため、Crystal Reportsデータベースに直接接続してはいけません)。

プロジェクトが「プレビューモード」などのブラウザでレポートを表示する必要がない場合は、ここで私はこのような状況のために書いたカスタムのActionResultです:クリスタルレポートので

public class PdfReportResult : ViewResult 
{ 
    /// <summary> 
    /// Crystal Report filename 
    /// </summary> 
    public string reportFileName { get; set; } 

    /// <summary> 
    /// DataSet used in the report 
    /// </summary> 
    public DataSet reportDataSet { get; set; } 

    /// <summary> 
    /// Report parameters 
    /// </summary> 
    IDictionary<string, object> parameters { get; set; } 

    public PdfReportResult(string reportFileName, DataSet reportDataSet, IDictionary<string, object> parameters) 
    { 
    if (string.IsNullOrEmpty(reportFileName)) 
     throw new ArgumentException("Report filename not informed", "reportFileName"); 

    if (reportDataSet == null) 
     throw new ArgumentException("DataSet not informed", "reportDataSet"); 

    if (parameters == null) 
     this.parameters = new Dictionary<string, object>(); 
    else 
     this.parameters = parameters; 

    this.reportDataSet = reportDataSet; 
    this.reportFileName = reportFileName; 
    } 

    public PdfReportResult(string reportFileName, DataSet reportDataSet) 
    : this(reportFileName, reportDataSet, null) 
    { } 

    public override void ExecuteResult(ControllerContext context) 
    { 
    if (context == null) 
     throw new ArgumentNullException("context"); 

    if (string.IsNullOrEmpty(this.ViewName)) 
     this.ViewName = context.RouteData.GetRequiredString("action"); 

    // Alias to make the code more legible 
    HttpResponseBase response = context.HttpContext.Response; 

    // Generating the report 
    using (ReportDocument report = new ReportDocument()) 
    { 
     // Load the report 
     report.Load(context.HttpContext.Server.MapPath("~/Reports/" + reportFileName)); 

     // Set the dataset 
     report.SetDataSource(reportDataSet); 

     // Set the parameters (if any) 
     foreach (var parameter in parameters) 
     report.SetParameterValue(parameter.Key, parameter.Value); 

     // Send back the PDF to the user 
     using (MemoryStream oStream = (MemoryStream)report.ExportToStream(ExportFormatType.PortableDocFormat)) 
     { 
     response.Clear(); 
     response.Buffer = true; 
     response.AddHeader("Content-Disposition", this.ViewName + ".pdf"); 
     response.ContentType = "application/pdf"; 
     response.BinaryWrite(oStream.ToArray()); 
     response.End(); 
     } 
    } 
    } 
} 
2

は、サーバーコントロールである場合、レポートを表示するにはWebページ/ユーザーコントロールが必要です。そして、このwebform/user controlをmvcのviewsフォルダの中に置かないと、CrViewerの404で壊れたボタンが表示されます。また、剃刀ビューでIframeを使用してレポートを表示することもできます。以下は作業モデル[VS2010]です。

ステップ1:Crystalレポートの設定
1. Webサイトのルートディレクトリに最上位のフォルダを作成します。
2. Crystalレポート.rptファイルをこのフォルダに入れます。
3.このフォルダにWebページ(.aspx)を追加します。このページはレポートビューアページとして機能します。このページにCrystalReportViewerコントロールを追加します。

4.アセンブリの登録は、aspxページの上部に追加されます。

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>. 
  1. のCrystalReportViewerのバージョンを確認してください。このためには、VSサイドバーのツールボックスから「項目を選択」を選択します。このバージョンとCrystalDecisions.Webバージョンをaspxページの上に比較してください。両方が同じ場合はそのままにしてください。そうでない場合は、CRViewerと同じアセンブリ登録バージョンを変更してください。

  2. ウェブサイトルートフォルダの下のweb.configファイルに移動します。タグの下の 'CrystalDecisions'で始まるアセンブリをチェックしてください。 CrystalReportViewerバージョン(ここではVersion = 13.0.2000.0)と同じバージョンを変更してください。

ステップ2:コントローラの設定、アクション&ビュー
1.レポートのコントローラクラスで新しいアクションを追加します。
2.データベース/ファイルからデータをロードするために必要な手順を記述します。
3.データをセッションに設定します。
4.この操作のビューを追加しないでください。代わりにResponse.Redirectメソッドを使用します。

public class ReportController : Controller 
    { 
    public ActionResult reportView(string id) 
    { 
    Session["ReportSource"] = GetdataFromDb(); 
    Response.Redirect("~/Reports/WebForm1.aspx"); 
    return View(); 
    } 
    } 
  1. .aspxページにページロードイベントを追加します。

    protected void Page_Load(object sender, EventArgs e) 
    { 
    CrystalDecisions.CrystalReports.Engine.ReportDocument report = 
    new CrystalDecisions.CrystalReports.Engine.ReportDocument(); 
    report.Load(Server.MapPath("~/Reports/CR_report.rpt")); 
    report.SetDataSource(Session["ReportSource"]); 
    crViewer.ReportSource =report; 
    } 
    

は、ステップ3:リトルを避けるためにGlobal.asaxの
1にハック「セッション状態はセッションIDを作成しましたが、応答がすでにによってフラッシュされたので、それを保存することはできませんアプリケーション "
エラーまたは"空白の水晶レポートページ出力 "次のコードをGlobal.asaxに追加します。

void Session_Start(object sender, EventArgs e) 
    { 
    string sessionId = Session.SessionID; 
    } 

今、あなたは、Crystalレポートを表示するには、ReportControllerでreportView()アクションを呼び出すことができます。
素敵な一日を!

関連する問題