To programmatically run and export a ReportViewer report to HTML in a .NET application, you can use the Microsoft ReportViewer control and its associated APIs. Here's a step-by-step guide on how to achieve this:

  1. Install ReportViewer Control: If you haven't already installed the ReportViewer control, you can do so using NuGet. Open the Package Manager Console and run the following command:

    Install-Package Microsoft.ReportingServices.ReportViewerControl.WebForms
  2. Add ReportViewer Control to Web Form: In your ASP.NET web form (.aspx), add the ReportViewer control to the page.

    <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> <rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
  3. Programmatically Load and Export the Report: In your code-behind file (e.g., Default.aspx.cs), you can programmatically load the report and export it to HTML.

    using Microsoft.Reporting.WebForms; using System.IO; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Load the report definition (RDLC file) string reportPath = Server.MapPath("~/Reports/YourReport.rdlc"); ReportViewer1.LocalReport.ReportPath = reportPath; // Set the report parameters, if any ReportParameter[] parameters = new ReportParameter[] { new ReportParameter("ParameterName", "ParameterValue") // Add other parameters as needed }; ReportViewer1.LocalReport.SetParameters(parameters); // Refresh the report data (optional) ReportViewer1.LocalReport.Refresh(); // Export the report to HTML format byte[] reportContent = ReportViewer1.LocalReport.Render("HTML"); string htmlFilePath = Server.MapPath("~/Reports/YourReport.html"); File.WriteAllBytes(htmlFilePath, reportContent); // Display the HTML file content or do further processing // ... } }

    In this example, we load the report definition (RDLC file) using the ReportViewer.LocalReport.ReportPath property. We set any required report parameters using the SetParameters method. Next, we refresh the report data (optional, if the report requires updated data).

    Finally, we export the report to HTML using the ReportViewer.LocalReport.Render method with the "HTML" format. The resulting HTML content is saved to a file on the server using File.WriteAllBytes. You can then use the htmlFilePath to read or display the HTML content or perform any other necessary operations.

Remember to adjust the code according to your specific report and application requirements. Additionally, ensure that you have appropriate permission to write files to the specified file path on the server.

Have questions or queries?
Get in Touch