تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
طباعة تقارير Report Viewer مباشرة
#1
أولأ : أضف للمشروع Class
وأضف الكود التالي
كود :
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms

Public Class Demo
   Implements IDisposable
   Private m_currentPageIndex As Integer
   Private m_streams As IList(Of Stream)

   Public Function LoadSalesData(ByVal aa As String) As DataTable
       Dim dtdb As New DataTable

       dtdb.Clear()
       Conn.Open()
       Dim da As New OleDbDataAdapter(aa, Conn)
       da.Fill(dtdb)
       Conn.Close()


       ' Create a new DataSet and read sales data file
       ' data.xml into the first DataTable.
       'Dim dataSet As New DataSet()
       'dataSet.ReadXml("..\..\data.xml")
       'Return DataSet.Tables(0)
       Return dtdb
   End Function

   ' Routine to provide to the report renderer, in order to
   ' save an image for each page of the report.
   Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
       Dim stream As Stream = New MemoryStream()
       m_streams.Add(stream)
       Return stream
   End Function

   ' Export the given report as an EMF (Enhanced Metafile) file.
   Private Sub Export(ByVal report As LocalReport, ByVal w As Single, ByVal h As Single, ByVal t As Single, ByVal l As Single, ByVal r As Single, ByVal b As Single)
       Dim deviceInfo As String = "<DeviceInfo>" & _
           "<OutputFormat>EMF</OutputFormat>" & _
           "<PageWidth>" & w & "in</PageWidth>" & _
           "<PageHeight>" & h & "in</PageHeight>" & _
           "<MarginTop>" & t & "in</MarginTop>" & _
           "<MarginLeft>" & l & "in</MarginLeft>" & _
           "<MarginRight>" & r & "in</MarginRight>" & _
           "<MarginBottom>" & b & "in</MarginBottom>" & _
           "</DeviceInfo>"
       Dim warnings As Warning()
       m_streams = New List(Of Stream)()
       report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
       For Each stream As Stream In m_streams
           stream.Position = 0
       Next
   End Sub

   ' Handler for PrintPageEvents
   Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
       Dim pageImage As New Metafile(m_streams(m_currentPageIndex))

       ' Adjust rectangular area with printer margins.
       Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX), _
                                         ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
                                         ev.PageBounds.Width + 20, _
                                         ev.PageBounds.Height)

       ' Draw a white background for the report
       ev.Graphics.FillRectangle(Brushes.White, adjustedRect)

       ' Draw the report content
       ev.Graphics.DrawImage(pageImage, adjustedRect)

       ' Prepare for the next page. Make sure we haven't hit the end.
       m_currentPageIndex += 1
       ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
   End Sub

   Private Sub Print(ByVal np As String)
       If m_streams Is Nothing OrElse m_streams.Count = 0 Then
           Throw New Exception("Error: no stream to print.")
       End If
       Dim printDoc As New PrintDocument()
       If Not printDoc.PrinterSettings.IsValid Then
           Throw New Exception("Error: cannot find the default printer.")
       Else

           AddHandler printDoc.PrintPage, AddressOf PrintPage
           m_currentPageIndex = 0
           printDoc.PrinterSettings.PrinterName = np
           printDoc.Print()

       End If
   End Sub
   Private Sub Printprev()
       If m_streams Is Nothing OrElse m_streams.Count = 0 Then
           Throw New Exception("Error: no stream to print.")
       End If
       Dim printDoc As New PrintDocument()
       Dim printpr As New PrintPreviewDialog
       'If Not printDoc.PrinterSettings.IsValid Then
       '    Throw New Exception("Error: cannot find the default printer.")
       'Else

       AddHandler printDoc.PrintPage, AddressOf PrintPage
       m_currentPageIndex = 0
       'printDoc.PrinterSettings.PrinterName = np
       printpr.WindowState = FormWindowState.Maximized
       printpr.StartPosition = FormStartPosition.CenterScreen
       printpr.ClientSize = New Size(600, 600)

       printpr.Document = printDoc
       printpr.PrintPreviewControl.Zoom = 1.0
       printpr.ShowDialog()

       'End If
   End Sub

   ' Create a local report for Report.rdlc, load the data,
   ' export the report to an .emf file, and print it.
   Public Sub Run(ByVal report As LocalReport, ByVal a As String, ByVal np1 As String, ByVal w As Single, ByVal h As Single, ByVal t As Single, ByVal l As Single, ByVal r As Single, ByVal b As Single)

       report.DataSources.Add(New ReportDataSource("DataSet1", LoadSalesData(a)))
       Export(report, w, h, t, l, r, b)
       Print(np1)
   End Sub

   Public Sub Run2(ByVal report As LocalReport, ByVal a As String, ByVal w As Single, ByVal h As Single, ByVal t As Single, ByVal l As Single, ByVal r As Single, ByVal b As Single)

       report.DataSources.Add(New ReportDataSource("DataSet1", LoadSalesData(a)))
       Export(report, w, h, t, l, r, b)
       Printprev()
   End Sub

   Public Sub Dispose() Implements IDisposable.Dispose
       If m_streams IsNot Nothing Then
           For Each stream As Stream In m_streams
               stream.Close()
           Next
           m_streams = Nothing
       End If
   End Sub

 
End Class


ثانياً: لطباعة أي تقرير مباشرة مثلاً من خلال النقر على زر

كود :
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

       Dim rep As New LocalReport
       rep.DataSources.Clear()
       rep.ReportEmbeddedResource = "printdirect2.Report1.rdlc"
     
       Dim par1 As New ReportParameter("par1", "بسم الله الرحمن الرحيم") : rep.SetParameters(par1)
       Dim Print As New Demo
       Print.Run(rep, "select * from Customers", 8.5, 11, 0.25, 0.25, 0.25, 0.25)
   End Sub
شرح الكود 
1- عمل متغير للريبورت
2- محو أي داتا سورس ليه
3- نوضح اسم المشروع واسم التقرير المراد استدعاؤه
4- مثال لعمل بارميتر
5- انشاء متغير من الكلاس اللي انشـأناه
6- ركز شويه عشان أنا اختصرتها خالص
السطر ده
       Print.Run(rep, "select * from Customers", 8.5, 11, 0.25, 0.25, 0.25, 0.25)
متغير الريبورت ثم جملة الاستعلام ثم عرض التقرير ثم الارتفاع ثم الهوامش من الأربعة جوانب 
والمقاسات بالبوصة



ثانياً: لمعاينة الطباعة بدون أداة Report
بدل Print.Run
نكتب 
Print.Run2

وبالتوفيق دايما

الرد }}}
تم الشكر بواسطة: asemshahen5 , Hamza lads , عبد الهادي بهاب , melad2002



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم