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

时间:2016-11-10 08:21:25

标签: c# sql-server asp.net-mvc ado.net rdlc

我正在进行mvc报道,而且我很陌生。我试图创建一个报告,我已经使用rdlc完成了。一切运作良好,可以导出为各种格式。使用rdlc时我的问题是我们需要先设计并绑定它。如何创建一个空的rdlc模板,设计并以编程方式将其与数据集绑定。

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

控制器文件

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 server调用的数据填充它。

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

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您想要从空白的RDLC创建报告。您必须在设计时告诉RDLC文件有关数据的信息。您可以通过添加其他表中的列或列来自定义设计时的报表,也可以进行连接。

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

答案 1 :(得分:2)

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

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

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

  2. RDLC包含可以从各种XML构建器类生成的XML标记(有关详细信息,请参阅MSDN Report Definition)。

  3. 因此,您可以先创建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标记,总结如下。 / p>

    1. 创建并添加DataSet以便在DataSources元素中使用。
    2. 创建报告正文和报告项目元素。
    3. 根据Tablix内的表格结构创建TablixCornerDataSet元素。
    4. 根据每个列数据类型创建包含TablixColumn控件的TablixRowTablixCellTextbox个元素。 Value控件中的Textbox元素应包含数据库绑定列的表达式=Fields!(ColumnName).Value
    5. 创建报告属性元素(数据集查询,字段等)
    6. 在正确生成所有报表元素后,将LoadReportDefinition添加到ASPX页面中。
    7. 或者,如果您已经知道整个RDLC元素结构,similar solution使用XmlWriter类可以生成从头开始构建报表所需的元素,然后将生成的XML重命名为RDLC扩展并将其绑定到{ {1}}控制(似乎需要一些时间来构建每个RDLC元素)。

      其他参考资料:

      Rendering an RDLC report in HTML in ASP.NET MVC

      Dynamic Reports with Reporting Services