做RDLC的子报表需要几个注意点
先贴一下源代码. 部分, 主要用来提供数据源.
public interface IMakeReport {
byte[] MakePdf(ReportDataModel reportModel, WReportTemplate tpl);
}
/// <summary>
/// 报表创建工具
/// </summary>
public class MakeReport : IMakeReport
{
ReportDataModel reportModel;
WReportTemplate tpl;
public virtual byte[] MakePdf(ReportDataModel reportModel, WReportTemplate tpl)
{
this.reportModel = reportModel;
this.tpl = tpl;
var ReportTemplateFileDirectory = GConfig.GetConfigByCache("ReportTemplateFileDirectory") as string;
LocalReport report = new LocalReport();
//设置需要打印的报表的文件名称。
report.EnableExternalImages = true;
report.ShowDetailedSubreportMessages = true;
report.ReportPath = ReportTemplateFileDirectory + tpl.MasterTplFileName;
//创建要打印的数据源
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;
string extension;
string outType = "PDF";//PDF,Word,Excel 都可以
byte[] bytes = report.Render(outType,
null,
out mimeType,
out encoding,
out extension,
out streamids,
out warnings);
return bytes;
// 参考http://blog.youkuaiyun.com/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<WDrugSensitivityTestReport> data = reportModel.ThreeDetails.Where(a => a.ReportDetailId == ReportDetailId).ToList();
if (data==null)
{
data = new List<WDrugSensitivityTestReport>();
}
//这里的ThreeDetails 是子报表中的数据集名称
ReportDataSource threeDetails = new ReportDataSource("ThreeDetails",
data.ToArray()
);
e.DataSources.Add(threeDetails);
}
}
下面是RDLC模版文件的一些注意事项.