Saturday, January 27, 2007

Exporting a report without preview in PDF format

Here is the code to export a report in PDF format without preview, but first of all the DataSet of rdl report must have the same name of the Datasource, that is, if the datasource name is "DataSource1" the name of the Dataset must be "DataSource1":
Imports FirebirdSql.Data.FirebirdClient
Imports Microsoft.Reporting.WinForms
Imports System.IO
Private Sub EsportaPDFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EsportaPDFToolStripMenuItem.Click
If Me.TextBox1.Text = "" Or Me.TextBox2.Text = "" Then
MsgBox("Perform before the search", MsgBoxStyle.Critical)
Exit Sub
End If
Label1.Visible = True
Me.Refresh()
Report(My.Application.Info.DirectoryPath & "\AnteRicercaTipo.rdl", "Biblioteca")
EsportaPDF()
RepTipo.ReportViewer1.Reset()
Label1.Visible = False

End Sub

Private Sub Report(ByVal filename As String, ByVal table As String)

Dim RepDS As New ReportDataSource

Dim dv As New System.Data.DataView

dv = DataSet1.Tables(table).DefaultView

RepDS.Name = "DataSource1"
RepDS.Value = dv
RepTipo.ReportViewer1.LocalReport.DataSources.Clear()
RepTipo.ReportViewer1.LocalReport.DataSources.Add(RepDS)

RepTipo.ReportViewer1.LocalReport.ReportPath = filename

End Sub

Private Sub EsportaPDF()


Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim bytes As Byte()

bytes = RepTipo.ReportViewer1.LocalReport.Render("PDF", _
Nothing, mimeType, _
encoding, extension, streamids, warnings)

Dim fs As New FileStream("c:\outputvb.pdf", FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
End Sub

No comments:

Post a Comment