2012-07-03 69 views
0

我正在使用一個ASP.NET MVC 2項目,它使用經典的ASP.NET WebForm連線顯示Crystal Report Viewer。ASP.NET MVC2中的Crystal Report視圖

這一切工作得很好。 問題是,我在CrystalReportViewer實例上設置了參數並將其渲染後,顯示的報告始終指示缺少參數值。

有沒有其他人遇到過這個?

回答

0

我不知道它是否會對你有所幫助,但我建議你在視圖上使用服務器控件返回一個不穩定的文件。您可以使用水晶報告導出PDF或DOC文件並將其回覆給客戶端。您可以嘗試使用這個自定義操作結果晶報道:

public class CrystalReportResult : FileResult 
{ 
    private const string CONTENTTYPE = "application/pdf"; 
    private const int BUFFERSIZE = 0x1000; 

    private Stream FileStream { get; set; } 
    private DataSet DataSource { get; set; } 
    private string NomeArquivoRpt { get; set; } 
    private string Diretorio { get; set; } 

    public FormaAbrirPdf AbrirComo { get; set; } 

    public CrystalReportResult(string nomeArquivoRpt, DataSet dataSource) 
     : base(CONTENTTYPE) 
    { 
     DataSource = dataSource; 
     NomeArquivoRpt = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, nomeArquivoRpt); 
     Diretorio = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp"); 
     AbrirComo = FormaAbrirPdf.ComoDownload; 

     if (!Directory.Exists(Diretorio)) 
     { 
      Directory.CreateDirectory(Diretorio); 
     } 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     var response = context.HttpContext.Response; 
     var arquivoPdf = Path.Combine(Diretorio, String.Format("{0}.pdf", Guid.NewGuid())); 
     var relatorioCrystal = new ReportClass { FileName = NomeArquivoRpt }; 

     relatorioCrystal.Load(); 
     relatorioCrystal.SetDataSource(DataSource); 

     if (AbrirComo == FormaAbrirPdf.ComoDownload) 
     { 
      relatorioCrystal.ExportToDisk(ExportFormatType.PortableDocFormat, arquivoPdf); 
      FileStream = new FileStream(arquivoPdf, FileMode.Open, FileAccess.ReadWrite); 

      var tamanho = (int) FileStream.Length; 
      var ms = new MemoryStream(tamanho); 

      FileStream.Read(ms.GetBuffer(), 0, tamanho); 
      ms.Close(); 

      response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
      response.OutputStream.Flush(); 
      response.OutputStream.Close(); 

      response.AddHeader("content-disposition", 
           String.Format("attachment;filename={0}", arquivoPdf)); 
     } 
     else 
     { 
      FileStream = relatorioCrystal.ExportToStream(ExportFormatType.PortableDocFormat); 
      response.AddHeader("content-type", CONTENTTYPE); 
     } 

     WriteFile(response); 
     FileStream.Close(); 

     if (AbrirComo != FormaAbrirPdf.ComoDownload) return; 

     File.Delete(arquivoPdf); 
    } 

    protected override void WriteFile(HttpResponseBase response) 
    { 
     var outputStream = response.OutputStream; 
     using (FileStream) 
     { 
      var buffer = new byte[BUFFERSIZE]; 

      while (true) 
      { 
       var bytesRead = FileStream.Read(buffer, 0, BUFFERSIZE); 
       if (bytesRead == 0) 
       { 
        break; 
       } 
       outputStream.Write(buffer, 0, bytesRead); 
      } 
     } 
    } 
} 

和格式進行下載:

public enum FormaAbrirPdf 
{ 
    NoNavegador, // open on the browser 
    ComoDownload // with download 
} 

PS:該代碼是PT-BR

+0

感謝您的建議和代碼。這是一個很好的解決方案。 – cg0x1

+0

不錯,如果它可以幫助你解決你的問題,請檢查一下。也許它可以幫助其他人:) –