RDLC 在指定的位置 \***.rdlc 找不到子报表"SubReport1" 请验证该子报表是否已发布,其名称是否正确

版权声明:本文为 走错路的程序员 原创文章,欢迎转载。 https://blog.csdn.net/phker/article/details/82491696

引发这个错误的可能有几个原因.

其中一个是确实找不到rdlc文件, 路径确实没写对.
这里写图片描述

第二个,子报表有问题,无法正常显示, 也会报同样的错误. 建议先实现子报表的显示,然后再签入主报表, 会比较容易找问题. 子报表如果有问题, 主报表上只会显示这么个内容.不方便查找问题的原因.

第三个, 数据源名称不对.我们知道子报表的数据源需要在代码中单独配置加载的.
代码如下


        ReportDataModel reportModel;



        public void Dispose()
        {
            reportModel = null;
        }

        public virtual byte[] MakePdf(ReportDataModel reportModel, WTemplateConfig tpl)
        {
            this.reportModel = reportModel; 

            int i = 1;
            reportModel.WSWDrugTests.ForEach(a => a.SortCode = i++); //这个地方用在微生物分组分列上

            var ReportTemplateFileDirectory = CenterConfig.GetConfig("ReportTemplateFileDirectory") as string;

            LocalReport report = new LocalReport();
            //设置需要打印的报表的文件名称。

            report.EnableExternalImages = true;
            report.ShowDetailedSubreportMessages = true;

            report.ReportPath = ReportTemplateFileDirectory + tpl.MasterTplFileName;
            //动态修改列宽 end
            //创建要打印的数据源 
            ReportDataSource master = new ReportDataSource("Master", new List<ReportDataModel>() { reportModel });
            report.DataSources.Add(master);
            //if (reportModel.Details != null)
            //{
            //    ReportDataSource details = new ReportDataSource("Details", reportModel.Details);
            //    report.DataSources.Add(details);
            //}
            if (reportModel.WSWDetails != null)
            {
                ReportDataSource wswdetails = new ReportDataSource("WSWDetails", reportModel.WSWDetails);
                report.DataSources.Add(wswdetails);
            }

            report.SubreportProcessing += Report_SubreportProcessing;

            //刷新报表中的需要呈现的数据
            report.Refresh();

            Warning[] warnings;
            string[] streamids;

            string mimeType;
            string encoding="UTF-8";
            string extension;

            string outType = "PDF";//PDF,Word,Excel 都可以
            //string deviceInfo = "<DeviceInfo>" + 
            //                        "  <OutputFormat>" + outType + "</OutputFormat>" + 
            //                        "  <PageWidth>21cm</PageWidth>" + 
            //                        "  <PageHeight>29.7cm</PageHeight>" + 
            //                        "  <MarginTop>0.5in</MarginTop>" + 
            //                        "  <MarginLeft>1in</MarginLeft>" + 
            //                        "  <MarginRight>1in</MarginRight>" + 
            //                        "  <MarginBottom>0.5in</MarginBottom>" + 
            //                   "</DeviceInfo>";
            byte[] bytes = report.Render(outType,
                                        null,
                                        out mimeType,
                                        out encoding,
                                        out extension,
                                        out streamids,
                                        out warnings);

            report.Dispose();
            report = null;

            return bytes;

            // 参考http://blog.csdn.net/Ealing/article/details/15814647
            // 将报表的内容输出为指定格式的数据流。
            //string deviceInfo =
            //  "<DeviceInfo>" +
            //  "  <OutputFormat>EMF</OutputFormat>" +
            //  "  <PageWidth>8.5in</PageWidth>" +
            //  "  <PageHeight>11in</PageHeight>" +
            //  "  <MarginTop>0.25in</MarginTop>" +
            //  "  <MarginLeft>0.25in</MarginLeft>" +
            //  "  <MarginRight>0.25in</MarginRight>" +
            //  "  <MarginBottom>0.25in</MarginBottom>" +
            //   "</DeviceInfo>";
            // Warning[] warnings;
            // //将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
            // report.Render("Image", deviceInfo, CreateStream, out warnings);

            //下面是直接输出的代码
            //Response.Clear();
            //Response.Buffer = true;
            //Response.ContentType = mimeType;
            //Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "." + extension);
            //Response.BinaryWrite(bytes);

            //Response.Flush(); 


            //ReportViewer1.Dispose();
            //ReportViewer1 = null; 
        }

        private void Report_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {

            string ReportDetailId = e.Parameters["ReportDetailId"].Values[0];

            List<ReportDataWswDrugTest> data = reportModel.WSWDrugTests.Where(a => a.ReportDetailId == ReportDetailId).ToList();
            if (data==null)
            {
                data = new List<ReportDataWswDrugTest>();
            }
            //注意这里的 数据源名称是 ThreeDetails  这个数据源的名称是设计报表模板的时候的子报表中数据源名称, 要一字不差. 
            ReportDataSource threeDetails = new ReportDataSource("ThreeDetails",  data.ToArray() );
            e.DataSources.Add(threeDetails); 
        }

这里写图片描述

第三点, 子报表中的某些表达式,可能使用了错误的数据源名称, 一般发生在修改数据源名称的时候… 建议用文本文件编辑器打开rdlc 搜索 老的数据源名称,然后替换.可避免此类问题.

猜你喜欢

转载自blog.csdn.net/phker/article/details/82491696