RDLC 在指定的位置 \***.rdlc 找不到子報表"SubReport1" 請驗證該子報表是否已釋出,其名稱是否正確
阿新 • • 發佈:2018-11-16
引發這個錯誤的可能有幾個原因.
其中一個是確實找不到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 搜尋 老的資料來源名稱,然後替換.可避免此類問題.