0

我正在处理一个ASP.NET MVC 2使用经典ASP.NET WebForm连接来显示 Crystal Report Viewer 的项目。

这一切都很好。问题是,在我在CrystalReportViewer实例上设置参数并渲染它之后,显示的报告总是表明参数值丢失。

有人遇到过这种情况么?

4

1 回答 1

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

于 2012-07-03T14:01:27.423 回答