65.9K
CodeProject 正在变化。 阅读更多。
Home

读取 RPT 文件然后显示 PDF 格式的报告

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.73/5 (6投票s)

2005年10月24日

3分钟阅读

viewsIcon

47599

本文教你如何使用外部RPT文件而不是嵌入到DLL文件中的RPT文件。

引言

当我们在为Web应用程序开发报表时,RPT文件通常会嵌入到一个DLL文件中。因此,当我们想编辑报表时,我们必须重新编译项目。这是读取RPT文件然后将其显示为PDF的简单解决方案。

 

现在,让我们创建一个函数来导出并以PDF格式显示你的报表。

 

    Private Function PrintDocument(ByVal SourceReport As Object, ByVal FormatType As CrystalDecisions.Shared.ExportFormatType, ByVal PaperSize As CrystalDecisions.Shared.PaperSize, ByVal PaperOrientation As CrystalDecisions.Shared.PaperOrientation)

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        '仅供教育目的

        '有关此函数的解释,请参阅我之前的文章

        With SourceReport.FormatEngine.PrintOptions

            .PaperSize = PaperSize

            .PaperOrientation = PaperOrientation

        End With

        Dim st As System.IO.Stream

        Dim b() As Byte

        st = SourceReport.ExportToStream(FormatType)

        Page.Response.ClearHeaders()

        Page.Response.ContentType = "application/pdf"

        ReDim b(st.Length)

        st.Read(b, 0, CInt(st.Length))

        Page.Response.BinaryWrite(b)

        Page.Response.End()

    End Function

 

接下来,我们在页面加载时调用该函数。

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        '仅供教育目的

        '有关此函数的解释,请参阅我之前的文章

        Dim myRpt As New ReportDocument

        myRpt.Site = Me.Site

        试试

            myRpt.Load(MapPath("../RPTFiles/Training.Rpt"))

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)

        Catch ex As Exception

            Exit Sub

        End Try

    End Sub

 

 

完整的源代码是

 

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.ReportSource

Imports CrystalDecisions.Shared

 

Public Class ReportTraining

    继承 System.Web.UI.Page

 

#Region " Web Form Designer Generated Code "

 

    '这是Web窗体设计器所必需的调用。

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

 

    End Sub

 

    '注意:以下占位符声明是Web窗体设计器所必需的。

    '请勿删除或移动它。

    Private designerPlaceholderDeclaration As System.Object

 

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN:此方法调用是Web窗体设计器所必需的

        '请勿使用代码编辑器修改它。

        InitializeComponent()

    End Sub

 

#End Region

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '在此处放置用户代码以初始化页面

        Dim myRpt As New ReportDocument

        myRpt.Site = Me.Site

        试试

            myRpt.Load(Me.MapPath("../CPXPrint/Training.Rpt"))

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)

        Catch ex As Exception

            Exit Sub

        End Try

    End Sub

 

    Private Function PrintDocument(ByVal SourceReport As Object, ByVal FormatType As CrystalDecisions.Shared.ExportFormatType, ByVal PaperSize As CrystalDecisions.Shared.PaperSize, ByVal PaperOrientation As CrystalDecisions.Shared.PaperOrientation)

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        '仅供教育目的

        '有关此函数的解释,请参阅我之前的文章

        With SourceReport.FormatEngine.PrintOptions

            .PaperSize = PaperSize

            .PaperOrientation = PaperOrientation

        End With

        Dim st As System.IO.Stream

        Dim b() As Byte

        st = SourceReport.ExportToStream(FormatType)

        Page.Response.ClearHeaders()

        Page.Response.ContentType = "application/pdf"

        ReDim b(st.Length)

        st.Read(b, 0, CInt(st.Length))

        Page.Response.BinaryWrite(b)

        Page.Response.End()

    End Function

End Class

© . All rights reserved.