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

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

我正在做 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/

相关文章:

php - 为什么在将 MS SQL 数据转换为 MYSQL 时会出现无效字符?

javascript - IE和chrome在发送XMLHTTPRequest之前不会执行js代码,直到收到该请求的响应

javascript - 如何使用 javascript 勾选页面加载的复选框?

c# - 如何在图像上使用 Alpha 绘制颜色 - C#/.NET

c# - 在 ASP.NET Web 应用程序中突出显示菜单栏

sql-server - 使用声明和设置变量创建存储过程

sql - 为什么不能在一次事务中创建和删除表两次?

c# - 如何尝试将字符串转换为 Guid

c# - 直接返回值或创建临时变量之间的性能差异

asp.net - mvc 在没有 httpContext 或 Controller 上下文的情况下渲染部分 View