2012年1月30日 星期一

VB.NET Crystal Reports Export to PDF

參考1
參考2
參考3
參考4
參考5
參考6:C# Crystal Reports Export to Pdf
參考引用8
--
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO

'This call is required by the Web Form Designer.
     Private Sub InitializeComponent()
        'start - This code should be pasted under #Region Private Sub itializeComponent()
'rptProduct is the name of crystal report.
        Me.docProd = New rptProduct
        '
        'docProd
        '
        Me.docProd.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.DefaultPaperOrientation
        Me.docProd.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.DefaultPaperSize
        Me.docProd.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Upper
        Me.docProd.PrintOptions.PrinterDuplex = CrystalDecisions.Shared.PrinterDuplex.Default
'Ends here
  
'Declaration of report document
Protected WithEvents docProd As rptProduct

Private Sub CreatePDF()      
        Dim objDS As New DataSet
        Dim dfdoFile As New CrystalDecisions.Shared.DiskFileDestinationOptions
        Dim strServerPath As String
        Dim szFileName As String

    
            'Create dataset as per requirement

            docProd.SetDataSource(objDS.Tables(0))

            szFileName = Session.SessionID & ".pdf"         ' rptDailyCalls.pdf
            strServerPath = MapPath("~") & "\Report\"       ' Here the pdf file will be saved.
                     File.Delete(strServerPath & "\" & szFileName)   ' Delete file first
            dfdoFile.DiskFileName = strServerPath & "\" & szFileName
            With docProd
                .ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
                .ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
                .ExportOptions.DestinationOptions = dfdoFile
                .Export()
            End With
       'URL of the pdf file
 Response.Redirect("http://localhost/WebApplication1/" & szFileName ,Flase)
End Sub


--
引用:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
    Dim cryRpt As New ReportDocument

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
        CrystalReportViewer1.ReportSource = cryRpt
        CrystalReportViewer1.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New _
            DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
            CrDiskFileDestinationOptions.DiskFileName = _
                                        "c:\crystalExport.pdf"
            CrExportOptions = cryRpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            cryRpt.Export()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
  

沒有留言:

張貼留言