Saturday, January 27, 2007

Exporting a report without preview in Excel format

Here is the code to export a report in Excel 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 EsportaExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EsportaExcelToolStripMenuItem.Click

If Me.TextBox1.Text = "" Or Me.TextBox2.Text = "" Then
MsgBox("Pereform before the search", MsgBoxStyle.Critical)
Exit Sub
End If

Label1.Visible = True
'To visualize the label

Report(My.Application.Info.DirectoryPath & "\EsportaExcel.rdl", "Biblioteca")
'This statement is necessary to display e new report after precedent displaying

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.ReportPath = filename

End Sub

Private Sub EsportaExcel()

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("Excel", _
Nothing, mimeType, _
encoding, extension, streamids, warnings)

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

End Sub

No comments:

Post a Comment