ReportViewer Core

repository·master·Indexed 19 days ago

https://github.com/lkosson/reportviewercore

A port of Microsoft Reporting Services (Report Viewer) to .NET 6+ and .NET 8, enabling the use of RDLC reports in modern .NET applications including WinForms, ASP.NET Core, and Console apps. It provides the LocalReport class for programmatic rendering to formats like PDF, HTML4.0, and HTML5, and the ServerReport class for accessing reports hosted on a Reporting Services server. This is an independent recompilation and is not supported or developed by Microsoft.

Tokens
2.3K
Snippets
7
Records
9
Agent score
19%

What's inside ReportViewer Core

  1. Integrate ReportViewer Core into WinForms projects

    master

    To migrate a WinForms application from .NET Framework to .NET 6+, replace your existing Report Viewer references with the ones provided in this repository. You can continue to use Microsoft.Reporting.WinForms.ReportViewer as you normally would.

    For a reference implementation, see the ReportViewerCore.Sample.WinForms project for local report processing or ReportViewerCore.Sample.WinFormsServer for remote processing via a Reporting Services server.

    // Use the existing WinForms control pattern
    // Reference Microsoft.Reporting.WinForms.ReportViewer
  2. Render reports in ASP.NET Core applications

    master

    For ASP.NET Core, add a reference to Microsoft.Reporting.NETCore. This version is a stripped-down version of the WinForms implementation designed for web applications, web services, and batch processing (it does not include the UI). You must load and render reports programmatically using LocalReport.

    Note: There is no interactive, web-based report viewer provided, but you can render to HTML4.0 or HTML5 formats.

    Stream reportDefinition; // your RDLC from file or resource
    IEnumerable dataSource; // your datasource for the report
    
    LocalReport report = new LocalReport();
    report.LoadReportDefinition(reportDefinition);
    report.DataSources.Add(new ReportDataSource("source", dataSource));
    report.SetParameters(new[] { new ReportParameter("Parameter1", "Parameter value") });
    byte[] pdf = report.Render("PDF");
  3. How to design new RDLC reports in Visual Studio

    master

    Visual Studio does not include the Report Designer by default.

    1. Install the Extension:

    2. Handle Data Sources: .NET Core projects do not support .datasource files, and the wizard may fail to show your project classes. To fix this, create an .xsd file containing your type definitions and add it to your project. The Report Designer will then see a new datasource named after your XSD file.

    // Snippet to generate an XSD from your report classes
    var types = new[] { typeof(ReportItemClass1), typeof(ReportItemClass2), typeof(ReportItemClass3) };
    var xri = new System.Xml.Serialization.XmlReflectionImporter();
    var xss = new System.Xml.Serialization.XmlSchemas();
    var xse = new System.Xml.Serialization.XmlSchemaExporter(xss);
    foreach (var type in types)
    {
        var xtm = xri.ImportTypeMapping(type);
        xse.ExportTypeMapping(xtm);
    }
    using var sw = new System.IO.StreamWriter("ReportItemSchemas.xsd", false, Encoding.UTF8);
    for (int i = 0; i < xss.Count; i++)
    {
        var xs = xss[i];
        xs.Id = "ReportItemSchemas";
        xs.Write(sw);
    }
  4. Linux rendering workaround for PDF, TIFF, and XLS

    master

    Certain formats (PDF, TIFF, XLS) rely on Windows native libraries (Uniscribe) for font measurements. To run these on Linux or macOS:

    1. Install Wine 5.0 or newer (e.g., apt install wine on Debian Bullseye).
    2. Download the Windows version of the .NET runtime binaries from the official Microsoft site.
    3. Extract the binaries to a local directory (e.g., ~/dotnet-3.1-windows).
    4. Execute your application using wine64 with the Windows .NET runtime: wine64 ~/dotnet-3.1-windows/dotnet.exe YourApplication.dll.

    Note: If you encounter unsupported flags 00000020 in bcrypt, ensure you are not using an older version of Wine (like 4.1 from Debian Buster).

    wine64 ~/dotnet-3.1-windows/dotnet.exe YourApplication.dll
  5. Basic usage of LocalReport

    master

    To generate a report using LocalReport, you need to provide an RDLC report definition (as a Stream), a data source, and any required parameters. The report can then be rendered into various formats, such as PDF, by calling the Render method.

    Steps:

    1. Initialize a new LocalReport instance.
    2. Load the RDLC definition using LoadReportDefinition(Stream reportDefinition).
    3. Add data sources using DataSources.Add(new ReportDataSource(string name, IEnumerable dataSource)).
    4. Set report parameters using SetParameters(IEnumerable<ReportParameter> parameters).
    5. Render the report to a byte array using Render(string extension) (e.g., "PDF").
    Stream reportDefinition; // your RDLC from file or resource
    IEnumerable dataSource; // your datasource for the report
    
    LocalReport report = new LocalReport();
    report.LoadReportDefinition(reportDefinition);
    report.DataSources.Add(new ReportDataSource("source", dataSource));
    report.SetParameters(new[] { new ReportParameter("Parameter1", "Parameter value") });
    byte[] pdf = report.Render("PDF");
  6. Known limitations and troubleshooting

    master

    Limitations

    • Spatial SQL types: SqlGeography and SqlGeometry are not supported because they require Microsoft.SqlServer.Types (available only in .NET Framework).
    • Security: Expression sandboxing is not implemented. Do not run reports from untrusted sources.
    • WinForms Designer: The visual control designer is not supported. Add the ReportViewer control programmatically.
    • Single-file deployment: Avoid compiling as a single .exe. Roslyn needs to reference .NET and ReportViewer assemblies at runtime, which fails in single-file mode.
    • System.Drawing: Since .NET 7, System.Drawing is removed from non-Windows platforms. Use the Linux rendering workaround to mitigate issues with images.

    Troubleshooting NuGet Conflicts

    If you see Version conflict detected for "Microsoft.CodeAnalysis.Common", manually add the following versions to your project:

    • .NET 5: Microsoft.CodeAnalysis.CSharp.Workspaces 3.6.0 or Microsoft.CodeAnalysis.Common 3.6.0
    • .NET 6: Microsoft.CodeAnalysis.Common 4.0.1
  7. Consume Reporting Services (server-side) reports

    master

    To access reports hosted on a Reporting Services server, use the ServerReport class. You must provide the server URL, credentials, and the report path.

    ServerReport report = new ServerReport();
    report.ReportServerCredentials = new NetworkCredential("login", "password", "DOMAIN");
    report.ReportServerUrl = new Uri("http://localhost/ReportServer");
    report.ReportPath = "/Invoice";
    report.SetParameters(new[] { new ReportParameter("Date", DateTime.Now.Date.ToString()) });
    byte[] pdf = report.Render("PDF");
  8. Supported rendering formats

    master

    The following formats are supported on Windows, Linux, and macOS. Formats marked with an asterisk (*) require the Linux rendering workaround (using Wine) to handle font measurements on non-Windows platforms.

    * HTML4.0 / HTML5 / MHTML
    * PDF (*)
    * IMAGE (TIFF/EMF) (*)
    * EXCEL (Microsoft Excel 97/2003) (*)
    * EXCELOPENXML (Microsoft Excel Open XML)
    * WORD (Microsoft Word 97/2003) (*)
    * WORDOPENXML (Microsoft Word Open XML)
    * CSV
    * XML