RDLC中利用SubReport来处理多层商务对象,输出到PDF

RDLC可以使用Business Objects。不过,当BO有多层的时候,就不能调用类似=First(Fields!Report.UpdateTime.Value, "Product")的值。还好我们可以利用SubReport概念来个参数接力,此问题就迎刃而解了。

这个例子里有个BO: Contacts, 元素为Contact。假设已经建立。Main Report里面就要有个BO的DataSource,名字叫Contact。另外有个SubReport,显示Contact.People.Name, Contact.People.Career等信息。此例中一个Contact对应一个People,SubReport设立好Report Parameter = "Id"。Main Report里把参数传给SubReport。

CodeFile里面增加一个函数,用来处理SubReport:

    private void SubreportProcessing(object sender, Microsoft.Reporting.WebForms.SubreportProcessingEventArgs e)
    {
        int id= int.Parse(e.Parameters["Id"].Values[0]);
                    e.DataSources.Add(
                        new ReportDataSource("People",
                            new object[] { (PeopleRepository.GetById(id)).People}));
    }



在Page_Load的时候声明:

LocalReport lr = new LocalReport();
lr.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessing);


根据条件,动态调用Local Report

                    // set the report and datasource
                    lr.ReportPath = "Report.rdlc";
                    lr.DataSources.Add(new ReportDataSource("Contact", contacts));

//这2行用在MainReport: //p = new ReportParameter("CountryName", country.Name);
                    //lr.SetParameters(new ReportParameter[] { p });


输出到PDF

            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string fileNameExtension;

            byte[] bytes = lr.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streamids, out warnings);

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-Type", "application/pdf");
            Response.AddHeader("Content-Disposition", "attachment;filename=" + exportFilename + ";");

            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.Close();

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值