Jump to content

Exportar um relatório em PDF direto sem abrir o ReportView


Daniel Ribeiro Amaral

Recommended Posts

Isto é VB.Net mas em C# deve ser semelhante. Isto está muito simplificado e devem faltar variáveis/funções acessórias mas deve dar para perceber a ideia.

  Dim warnings As Warning()
        Dim streamids As String()
        Dim mimeType As String
        Dim encoding As String
        Dim filenameExtension As String

        Dim objDados As Microsoft.Reporting.WinForms.ReportDataSource
        Dim objDataSource As DataTable
        Dim PathComNomeMapa As String
        Dim SaveFileDialog As New SaveFileDialog


        Try
            Dim tipodoc As String = ""
            Dim pasta As String = ""

            numero_documento = numero_documento.Replace("/", ".")
            PathComNomeMapa = System.AppDomain.CurrentDomain.BaseDirectory() & "\" & pasta & "\" & tipodoc & numero_documento & ".pdf"


            If Not Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory() & "\" & pasta) Then
                Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory() & "\" & pasta)
            End If
            ReportViewer = New ReportViewer()

            ReportViewer.ProcessingMode = ProcessingMode.Local

            'Set Data Source for Report
            objDados = New Microsoft.Reporting.WinForms.ReportDataSource
            objDataSource = Dados
            objDados.Name = "LOGOCAR"
            objDados.Value = objDataSource
  

            'Reset the report
            ReportViewer.Reset()

  
            ReportViewer.LocalReport.LoadReportDefinition(New StreamReader(CalculaCaminhoReport(NomeMapa, lingua)))
            reportRDL = File.ReadAllText(CalculaCaminhoReport(NomeMapa, lingua))

            ReportViewer.LocalReport.DataSources.Add(objDados)

            ReportViewer.LocalReport.EnableExternalImages = True

            Dim i As Integer
            Dim j As Integer
            Dim Atributos() As String
            Dim oParametros As ReportParameterInfoCollection

            oParametros = ReportViewer.LocalReport.GetParameters()
            Dim parameters As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
            If Not Parametros Is Nothing Then
                For i = 1 To Parametros.GetUpperBound(0) + 1
                    If Not Parametros(i - 1) Is Nothing Then
                        Atributos = Split(Parametros(i - 1), "§")
                        For j = 0 To oParametros.Count - 1

                            If UCase(Atributos(0)) = UCase(oParametros(j).Name) Then
                                Dim parameter = New Microsoft.Reporting.WinForms.ReportParameter(oParametros(j).Name, Atributos(1))
                                parameters.Add(parameter)
                            END If
                        Next
                    End If
                Next
                ReportViewer.LocalReport.SetParameters(parameters)
            End If


            Dim st As TextReader
            Dim listaReaders As New ArrayList

            Try
                Dim lista As ArrayList = ExtraiSubReports(reportRDL)
                For Each subreport As String In lista
                    st = New StreamReader(CalculaCaminhoReport(subreport, lingua))
                    ReportViewer.LocalReport.LoadSubreportDefinition(subreport, st)
                    listaReaders.Add(st)
                Next

                AddHandler ReportViewer.LocalReport.SubreportProcessing, New SubreportProcessingEventHandler(AddressOf LocalReport_SubreportProcessing)

            Catch ex As Exception
                DbgException(ex)
            End Try

            ' Process and render the report 
            ReportViewer.RefreshReport()

            mimeType = ""
            encoding = ""
            Dim bytes As Byte() = ReportViewer.LocalReport.Render("Pdf", Nothing, mimeType, encoding, filenameExtension, streamids, warnings)

            Using fs As New FileStream(PathComNomeMapa, FileMode.Create)
                fs.Write(bytes, 0, bytes.Length)
                fs.Close()
            End Using

            ReportViewer.LocalReport.Dispose()
            ReportViewer.Dispose()

            Try
                For Each st In listaReaders
                    If Not st Is Nothing Then
                        st.Close()
                    End If
                Next

            Catch ex As Exception

            End Try

            Return PathComNomeMapa


        Catch ex As Exception
            DbgException(ex)
        End Try

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.