以编程方式生成和设计 Rdlc 文件

Generate and Design Rdlc file programmatically

我正在做 mvc 报告,我对它还很陌生。我正在尝试创建一个报告,我已经使用 rdlc 完成了它。一切正常,可以导出为各种格式。我的问题是,在使用 rdlc 时,我们需要先设计和绑定它。我如何创建一个空的 rdlc 模板,以编程方式设计并将其与数据集绑定。

我目前的工作(使用 空 rdlc 模板 - 刚刚创建了没有任何 table 的文件),

控制器文件,

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);

    }

模型文件,

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;
        }
    }

查看文件,

<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>

数据在那里,但我只是不知道如何将它与 rdlc 连接。表示根据数据创建列并用从 sql 服务器调用的数据填充它。

TQVM 进阶。解释和示例或任何其他方法都会有所帮助。

如果我对你的问题的理解正确,你想从空白的 RDLC 创建报告。您必须在设计时将数据告知 RDLC 文件。您可以在设计时通过添加列或来自另一个 table 的列或进行连接来自定义报表。

Dynamic RDLC Generator through C# 将在那里从 RDLC 动态生成报告。由于完整的 ReportingEngine 是定制的,但非常通用。复制粘贴可能有助于生成报告。

您的问题暗示您需要在运行时模式下生成 RDLC 报告。你应该记住的事情:

  1. RDLC 报表查看器使用 Microsoft.ReportViewer.WebFormsMicrosoft.Reporting.WebForms 命名空间,它们利用 WebForms 逻辑代码来绑定和呈现报表。您可以使用部分视图作为 ASPX 页面的容器(使用单页或代码隐藏的页面)在 MVC 视图页面中呈现报告。

    注意:您可以在控制器操作方法中使用 ReportViewer 实例将 RDLC 呈现为 PDF 文件并返回 FileContentResult(参见 here)。

  2. RDLC 包含 XML 标签,这些标签可以从各种 XML 构建器 classes 生成(详情请参阅 MSDN Report Definition)。

因此,您可以先创建 ReportViewer 实例,例如:

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
    }
}

然后,使用Generating RDLC Dynamically for the Report Viewer Local Report示例中的步骤(WebForms在事件处理用法方面类似于WinForms,因此可能适用于ASPX页面)创建相应的XML标签,生成报表结构,总结如下。

  1. 创建并添加 DataSet 以便在 DataSources 元素中使用。
  2. 创建报表正文和报表项元素。
  3. 根据DataSet内的table结构创建TablixTablixCorner个元素。
  4. 创建 TablixColumnTablixRowTablixCell 元素,其中包括 Textbox 控件,具体取决于每个列的数据类型。 Textbox 控件内的 Value 元素应包含数据库绑定列的表达式 =Fields!(ColumnName).Value
  5. 创建报表 属性 个元素(数据集查询、字段等)
  6. 正确生成所有报表元素后,将 LoadReportDefinition 添加到 ASPX 页面。

或者,如果您已经知道整个 RDLC 元素结构,similar solution 使用 XmlWriter class 可以生成从头开始构建报告所需的元素,然后重命名生成的 XML 到 RDLC 扩展并将其绑定到 ReportViewer 控件(似乎需要一些时间来构建每个 RDLC 元素)。

其他参考资料:

Rendering an RDLC report in HTML in ASP.NET MVC

Dynamic Reports with Reporting Services