RDLC-reportviewercore
[删除(380066935@qq.com或微信通知)]
lkosson/reportviewercore: Recompilation of Microsoft.ReportViewer for .NET Core 3.1+ (github.com)
ReportViewer Core
This project is a port of Microsoft Reporting Services (Report Viewer) to .NET Core 3.1+. It is feature-complete and ready for production use, but keep in mind it is not officially supported by Microsoft.
Why
With WinForms inclusion in .NET Core 3.1 and .NET 5 as a replacement for .NET Framework, it became feasible to port existing business desktop applications to .NET Core SDK to benefit from new C# and JIT features. Microsoft team stated on multiple occasions (dotnet/aspnetcore#1528, dotnet/aspnetcore#12666, dotnet/aspnetcore#22304, dotnet/docs#9607) that there are no plans to have official Reporting Services / ReportViewer package for .NET Core, which is a showstopper for applications using this technology for printing and reporting. The goal of this project is to provide transitional solution for such applications, until existing reports are reimplemented using more modern technology.
How to use
You should be able to replace references to Report Viewer in your WinForms project with ones provided in this repo and use Microsoft.Reporting.WinForms.ReportViewer
as usual. See project ReportViewerCore.Sample.WinForms
for a simplified example using local report processing and ReportViewerCore.Sample.WinFormsServer
for remote processing using Reporting Services server.
For ASP.NET Core applications, add reference to Microsoft.Reporting.NETCore
, which is based on WinForms version with stripped UI, and load/render report programmatically. See project ReportViewerCore.Console
for an example or use following code as a starting point:
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");
For consuming Reporting Services (server-side) reports, use:
ServerReport report = new ServerReport();
report.ReportServerCredentials.NetworkCredentials = 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");
or see project ReportViewerCore.WinFormsServer
for more complete example.
There is no interactive, web-based report viewer provided in this project, but there are HTML4.0
and HTML5
rendering formats available. HTML5
format has been modified to also work without JavaScript. See ReportViewerCore.Sample.AspNetCore
project for a simple demo.
Designing new reports
Visual Studio 2019 (version 16.9 and similar) does not include Report Designer by default. There is an extension provided by Microsoft called "Microsoft RDLC Report Designer" you need to be able to open and modify your reports. For Visual Studio 2022, you need "Microsoft RDLC Report Designer 2022" extension.
Even after installing the extension, new dataset wizard fails to show classes from your project and .NET Core projects don't have .datasource
file support either. The workaround is to create and add .xsd
file to your project with definitions of types you want to use in your reports. You can either create this file by hand or use the following snippet to produce one for all the classes you need:
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);
}
After including ReportItemSchemas.xsd
file in your project, Report Designer should see a new datasource called ReportItemSchemas
which you can use to add a dataset to your report.
What works
- RDLC file loading and compiling
- Local data sources
- Parameter passing
- All rendering formats, including PDF and XLS
- WinForms report preview
- Remote processing using Reporting Services
- Linux and MacOS support
- MSChart control
Supported rendering formats
All formats are supported on Windows, Linux and Mac OS. For formats marked with asterisk (*), see "Linux rendering workaround" section below.
- 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)
How to use
You should be able to replace references to Report Viewer in your WinForms project with ones provided in this repo and use Microsoft.Reporting.WinForms.ReportViewer
as usual. See project ReportViewerCore.Sample.WinForms
for a simplified example using local report processing and ReportViewerCore.Sample.WinFormsServer
for remote processing using Reporting Services server.
For ASP.NET Core applications, add reference to Microsoft.Reporting.NETCore
, which is based on WinForms version with stripped UI, and load/render report programmatically. See project ReportViewerCore.Console
for an example or use following code as a starting point:
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");
For consuming Reporting Services (server-side) reports, use:
ServerReport report = new ServerReport();
report.ReportServerCredentials.NetworkCredentials = 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");
or see project ReportViewerCore.WinFormsServer
for more complete example.
There is no interactive, web-based report viewer provided in this project, but there are HTML4.0
and HTML5
rendering formats available. HTML5
format has been modified to also work without JavaScript. See ReportViewerCore.Sample.AspNetCore
project for a simple demo.
Designing new reports
Visual Studio 2019 (version 16.9 and similar) does not include Report Designer by default. There is an extension provided by Microsoft called "Microsoft RDLC Report Designer" you need to be able to open and modify your reports. For Visual Studio 2022, you need "Microsoft RDLC Report Designer 2022" extension.
Even after installing the extension, new dataset wizard fails to show classes from your project and .NET Core projects don't have .datasource
file support either. The workaround is to create and add .xsd
file to your project with definitions of types you want to use in your reports. You can either create this file by hand or use the following snippet to produce one for all the classes you need:
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);
}
After including ReportItemSchemas.xsd
file in your project, Report Designer should see a new datasource called ReportItemSchemas
which you can use to add a dataset to your report.
What works
- RDLC file loading and compiling
- Local data sources
- Parameter passing
- All rendering formats, including PDF and XLS
- WinForms report preview
- Remote processing using Reporting Services
- Linux and MacOS support
- MSChart control
Supported rendering formats
All formats are supported on Windows, Linux and Mac OS. For formats marked with asterisk (*), see "Linux rendering workaround" section below.
- 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)