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

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

Controller 文件

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处于高级状态。说明和示例或任何其他方法将很有帮助。

最佳答案

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

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

关于c# - 以编程方式生成和设计Rdlc文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40522881/

10-12 18:24