Saturday, January 27, 2007

MS reportviewer. Printing a report without preview

Here is the code to print a report 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
Imports System.Threading
Private Sub StampaRicercaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RicercaTipoStampaToolStripMenuItem.Click
If Me.TextBox1.Text = "" Or Me.TextBox2.Text = "" Then
MsgBox("Perform before the search", MsgBoxStyle.Critical)
Exit Sub
End If

Label1.Text = "Attendere," & vbCr & "stampa in corso..."
Label1.Visible = True

Label2.Visible = True
Report(My.Application.Info.DirectoryPath & "\AnteRicercaTipo.rdl", "Biblioteca")
Dim process As New Process()
process.StartInfo.FileName = "C:\outputvb.pdf"
process.StartInfo.CreateNoWindow = True
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo.Verb = "Print"


If Not process.CloseMainWindow() Then
End If

Label2.Visible = False

Label1.Visible = False
Me.WindowState = FormWindowState.Maximized
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.ReportPath = filename

End Sub

Private Sub EsportaPDF()

'L'istruzione si mette altrimenti la label non viene visualizzata
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)
End Sub

No comments:

Post a Comment