Imports System.IO


Public Class tsXtraReport
    Public Shared Sub ExportarAPDF(Plantilla() As Byte, Datos As Object, FicheroPDF As String, Optional Parametros As Hashtable = Nothing)
        Dim xr As DevExpress.XtraReports.UI.XtraReport
        Dim s As String = System.Text.Encoding.UTF8.GetString(Plantilla)
        Using sw As New StreamWriter(New MemoryStream())
            sw.Write(s)
            sw.Flush()
            xr = DevExpress.XtraReports.UI.XtraReport.FromStream(sw.BaseStream, True)
        End Using
        If xr.Parameters IsNot Nothing Then
            Dim pr As Global.DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").FirstOrDefault
            If pr IsNot Nothing Then
                pr.Value = Now
                pr.Visible = False
            End If
            If Parametros IsNot Nothing Then
                For Each entry As DictionaryEntry In Parametros
                    Dim par = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = entry.Key).FirstOrDefault
                    If par IsNot Nothing Then
                        par.Value = entry.Value
                        par.Visible = False
                    End If
                Next
            End If
        End If

        xr.DataSource = Datos
        xr.CreateDocument()
        xr.ExportToPdf(FicheroPDF)
    End Sub

    Public Shared Sub ExportarAxls(Plantilla() As Byte, Datos As Object, Ficheroxls As String, Optional Parametros As Hashtable = Nothing)
        Dim xr As DevExpress.XtraReports.UI.XtraReport
        Dim s As String = System.Text.Encoding.UTF8.GetString(Plantilla)
        Using sw As New StreamWriter(New MemoryStream())
            sw.Write(s)
            sw.Flush()
            xr = DevExpress.XtraReports.UI.XtraReport.FromStream(sw.BaseStream, True)
        End Using
        If xr.Parameters IsNot Nothing Then
            Dim pr As Global.DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").FirstOrDefault
            If pr IsNot Nothing Then
                pr.Value = Now
                pr.Visible = False
            End If
            If Parametros IsNot Nothing Then
                For Each entry As DictionaryEntry In Parametros
                    Dim par = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = entry.Key).FirstOrDefault
                    If par IsNot Nothing Then
                        par.Value = entry.Value
                        par.Visible = False
                    End If
                Next
            End If
        End If
        xr.DataSource = Datos
        xr.CreateDocument()
        xr.ExportToXls(Ficheroxls)

    End Sub
    Public Shared Sub ExportarAxlsx(Plantilla() As Byte, Datos As Object, Ficheroxlsx As String, Optional Parametros As Hashtable = Nothing)
        Dim xr As DevExpress.XtraReports.UI.XtraReport
        Dim s As String = System.Text.Encoding.UTF8.GetString(Plantilla)
        Using sw As New StreamWriter(New MemoryStream())
            sw.Write(s)
            sw.Flush()
            xr = DevExpress.XtraReports.UI.XtraReport.FromStream(sw.BaseStream, True)
        End Using
        If xr.Parameters IsNot Nothing Then
            Dim pr As Global.DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").FirstOrDefault
            If pr IsNot Nothing Then
                pr.Value = Now
                pr.Visible = False
            End If
            If Parametros IsNot Nothing Then
                For Each entry As DictionaryEntry In Parametros
                    Dim par = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = entry.Key).FirstOrDefault
                    If par IsNot Nothing Then
                        par.Value = entry.Value
                        par.Visible = False
                    End If
                Next
            End If
        End If
        xr.DataSource = Datos
        xr.CreateDocument()
        xr.ExportToXlsx(Ficheroxlsx)

    End Sub
    Public Shared Sub ExportarAcsv(Plantilla() As Byte, Datos As Object, Ficherocsv As String, Optional Parametros As Hashtable = Nothing)
        Dim xr As DevExpress.XtraReports.UI.XtraReport
        Dim s As String = System.Text.Encoding.UTF8.GetString(Plantilla)
        Using sw As New StreamWriter(New MemoryStream())
            sw.Write(s)
            sw.Flush()
            xr = DevExpress.XtraReports.UI.XtraReport.FromStream(sw.BaseStream, True)
        End Using
        If xr.Parameters IsNot Nothing Then
            Dim pr As Global.DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").FirstOrDefault
            If pr IsNot Nothing Then
                pr.Value = Now
                pr.Visible = False
            End If
            If Parametros IsNot Nothing Then
                For Each entry As DictionaryEntry In Parametros
                    Dim par = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = entry.Key).FirstOrDefault
                    If par IsNot Nothing Then
                        par.Value = entry.Value
                        par.Visible = False
                    End If
                Next
            End If
        End If
        xr.DataSource = Datos
        xr.CreateDocument()
        xr.ExportToCsv(Ficherocsv)

    End Sub


    Public Shared Function ExportarAPDF(Plantilla() As Byte, Datos As Object, Optional Parametros As Hashtable = Nothing) As MemoryStream
        Dim xr As DevExpress.XtraReports.UI.XtraReport
        Dim s As String = System.Text.Encoding.UTF8.GetString(Plantilla)
        Using sw As New StreamWriter(New MemoryStream())
            sw.Write(s)
            sw.Flush()

            xr = DevExpress.XtraReports.UI.XtraReport.FromStream(sw.BaseStream, True)
        End Using
        If xr.Parameters IsNot Nothing Then
            Dim pr As Global.DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").FirstOrDefault
            If pr IsNot Nothing Then
                pr.Value = Now
                pr.Visible = False
            End If
            If Parametros IsNot Nothing Then
                For Each entry As DictionaryEntry In Parametros
                    Dim par = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = entry.Key).FirstOrDefault
                    If par IsNot Nothing Then
                        par.Value = entry.Value
                        par.Visible = False
                    End If
                Next
            End If
        End If
        xr.DataSource = Datos
        xr.CreateDocument()
        Dim ms As New MemoryStream
        xr.ExportToPdf(ms)
        Return ms
    End Function
End Class