Generate and design Rdlc file programmatically

I am reporting on mvc and I am very new to this. I am trying to create a report and I have done it with rdlc. Everything works well, it can be exported to a different format. My problem when using rdlc is that we need to construct and link it first. How can I create an empty rdlc template, design and associate it with a dataset.

My work so far (using an empty rdlc template ) has just created a file without any table)

Controller file

public ActionResult Report(string id) { DB.Open(); LocalReport lr1 = new LocalReport(); string path1 = Path.Combine(Server.MapPath("~/Report"), "TestEmptyReport.rdlc"); lr1.ReportPath = path1; DataTable pc2a = new DataTable(); pc2a = DB.getDataSet().Tables[0]; pc2a.Columns.Add("Product Name"); pc2a.Columns.Add("Price"); pc2a.Columns.Add("Quantity"); ReportDataSource rdc = new ReportDataSource("DataSet1", pc2a); lr1.DataSources.Add(rdc); string reportType = id; string mimeType; string encoding; string fileNameExtension; string deviceInfo = "<DeviceInfo>" + "<OutputFormat>" + id + "</OutputFormat>" + "<PageWidth>8.5in</PageWidth>" + "<PageHeight>11in</PageHeight>" + "<MarginTop>0.5in</MarginTop>" + "<MarginLeft>1in</MarginLeft>" + "<MarginRight>1in</MarginRight>" + "<MarginBottom>0.5in</MarginBottom>" + "</DeviceInfo>"; Warning[] warnings; string[] streams; byte[] renderedBytes; renderedBytes = lr1.Render( reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); return File(renderedBytes, mimeType); } 

Model file

 public DataSet getDataSet() { string query = "SELECT * FROM tblproduct"; if (con.State.ToString() == "Open") { SqlDataAdapter ad = new SqlDataAdapter(query, con); DataSet ds = new DataSet("tblproduct"); ad.Fill(ds); return ds; } else { return null; } } 

View file,

 <div style="padding: 10px; border: 1px solid black"> <div><a href="@Url.Action("Report", new { id = "PDF" })">Get PDF Report</a></div> <div><a href="@Url.Action("Report", new { id = "Excel" })">Get Excel Report</a></div> <div><a href="@Url.Action("Report", new { id = "Word" })">Get Word Report</a></div> <div><a href="@Url.Action("Report", new { id = "Image" })">Get Image Report</a></div> 

There is data, but I just don’t know how to connect it to rdlc. The tool creates a column based on the data and populates it with the data called from the SQL server.

TQVM in advanced mode. An explanation and example or any other method will be useful.

+6
source share
2 answers

If I understood your question correctly, you wanted to create a report from an empty RDLC. You must tell the RDLC file about the data at design time. You can customize the report at design time by adding columns or columns from another table or make a join.

While the Dynamic RDLC Generator through C # would have to generate a report dynamically from the RDLC. Since the full ReportingEngine was custom made, but very general. Copy paste can help generate a report.

+2
source

Your question implies that you need to create an RDLC report at run time. What you should remember:

  • The RDLC report viewer uses the Microsoft.ReportViewer.WebForms and Microsoft.Reporting.WebForms namespaces, which use the WebForms logic code to bind and render the report. You can use a partial view that acts as a container for an ASPX page (either using one page or a page with code behind) to display a report on MVC view pages.

    Note. You can use the ReportViewer instance in the controller action method to render the RDLC as a PDF file and return a FileContentResult (see here ).

  • The RDLC contains XML tags that can be generated from various classes in the XML builder (for more information, see MSDN Report Definition ).

Therefore, you can first create an instance of ReportViewer, for example:

 using Microsoft.Reporting.WebForms; protected void Form_Load(Object sender, EventArgs e) { if (!IsPostBack) { ReportViewer1.ProcessingMode = ProcessingMode.Local; ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc"); // add your DataSet here var data = new DataTable(); // example data source var dataSource = new ReportDataSource(DataSetName, data); ReportViewer1.LocalReport.DataSources.Add(dataSource); // put rendering stuff here } } 

Then use the steps from the Example of creating an RDLC for a local View Viewer report (WebForms is similar to WinForms in terms of using event processing, so it can be applied on the ASPX page) to create the corresponding XML tags that generate the report structure, as shown below.

  • Create and add a DataSet to use in the DataSources element.
  • Create a report body element and report elements.
  • Create the Tablix and TablixCorner elements according to the table structure inside the DataSet .
  • Create TablixColumn , TablixRow and TablixCell that include Textbox controls based on the data types of the columns. The Value element inside the Textbox control must contain the expression =Fields!(ColumnName).Value for the columns bound to the database.
  • Creating report property elements (querying a dataset, field, etc.).
  • Add LoadReportDefinition to the ASPX page after creating all report elements.

Alternatively, if you already know the whole structure of RDLC elements, a similar solution using the XmlWriter class can generate the required elements to create a report from scratch, and then rename the generated XML Extension for RDLC and bind it to the ReportViewer control (to create each RDLC element it takes some time).

Additional links:

Providing RDLC report in HTML in ASP.NET MVC

Dynamic Reports with Reporting Services

+2
source

Source: https://habr.com/ru/post/1012201/


All Articles