Mit der folgende Methoden lässt sich ein Microsoft ReportViewer mit Daten aus einem XML File befüllen und anzeigen.
Den Report an sich kann man bequem im Visual Studio erstellen und abspeichern. Databindings und dergleichen sind nicht notwendig.
Die untere Methode ermöglicht das Speichern eines Reports als PDF-Datei.
Public Sub ShowReportViewer(ByVal aXMLFilename As String, ByVal aReportFilename As String, _ ByVal aDatasetName As String, ByVal aTableName As String, _ ByVal aDataTableIndex As Integer, _ Optional ByVal DoSaveReport As Boolean = False, Optional ByVal SaveReportAsFilename As String = "") If Not System.IO.File.Exists(aXMLFilename) Then MsgBox(String.Format("Die XML Datenquelle ""{0}"" wurde nicht gefunden!", aXMLFilename), MsgBoxStyle.Critical) Exit Sub End If If Not System.IO.File.Exists(aReportFilename) Then MsgBox(String.Format("Die Report Vorlage ""{0}"" wurde nicht gefunden!", aReportFilename), MsgBoxStyle.Critical) Exit Sub End If ' Create new DataSet and load Data from aXMLFilename into it Dim ds As New DataSet() ds.DataSetName = aDatasetName ds.ReadXml(aXMLFilename) ' Create Form Dim frm As New Windows.Forms.Form frm.StartPosition = Windows.Forms.FormStartPosition.CenterParent frm.Height = 400 frm.Width = 400 ' Create Report Data Source ' The most important part here is aDatasetName & "_" & aTablename Dim rds As New Microsoft.Reporting.WinForms.ReportDataSource(aDatasetName & "_" & aTableName, ds.Tables(aDataTableIndex)) ' Create ReportViewer Dim rv As New Microsoft.Reporting.WinForms.ReportViewer rv.Dock = Windows.Forms.DockStyle.Fill ' Add ReportViewer to Form frm.Controls.Add(rv) ' Load Report Definition File Dim fs As New System.IO.FileStream(aReportFilename, IO.FileMode.Open) rv.LocalReport.LoadReportDefinition(fs) ' Add Report Data Source rv.LocalReport.DataSources.Clear() rv.LocalReport.DataSources.Add(rds) rv.RefreshReport() ' Save Report as File? If DoSaveReport Then SaveReport(rv.LocalReport, SaveReportAsFilename) End If ' Finally Show Form frm.ShowDialog() End Sub
Public Sub SaveReport(ByVal aLocalReport As Microsoft.Reporting.WinForms.LocalReport, ByVal aFilename As String, Optional ByVal aRenderFormat As String = "PDF") ' Todo: Check if aFilename already exists and prompt user to overwrite/skip Dim warnings As Microsoft.Reporting.WinForms.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() Try bytes = aLocalReport.Render(aRenderFormat, Nothing, mimeType, encoding, extension, streamids, warnings) Dim fs As New IO.FileStream(aFilename, System.IO.FileMode.Create) fs.Write(bytes, 0, bytes.Length) fs.Close() Catch ex As Exception ' AddToLog(ex.Message) End Try End Sub
Beispielaufruf:
Public Sub Test() ShowReportViewer("C:\test.xml", "C:\repMain.rdlc", "NewDataSet", "Table", 0, True, "C:\test.pdf") End Sub
Ergänzung vom 17.02.2012:
Hier noch ein Beispiel, wie man einen Microsoft Report als eingebettete Ressource verwendet, den Report also nicht per Dateinamen angeben muss:
Dim bs As New System.Windows.Forms.BindingSource bs.DataMember = "dtReportDetails" bs.DataSource = Me Dim rds As New Microsoft.Reporting.WinForms.ReportDataSource("dsReport_dtReportDetails", bs) Dim rvMain As New Microsoft.Reporting.WinForms.ReportViewer rvMain.LocalReport.DataSources.Add(rds) rvMain.LocalReport.ReportEmbeddedResource = "Controlling.Neukunden.repSantaFuck.rdlc" rvMain.Dock = System.Windows.Forms.DockStyle.Fill rvMain.TabIndex = 0 rvMain.RefreshReport() Dim frm As New Windows.Forms.Form frm.Controls.Add(rvMain) frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen frm.WindowState = Windows.Forms.FormWindowState.Maximized frm.ShowDialog()
0 Antworten zu VB .NET ReportViewer