1.用户自定义模版 设置标签内对应的数据源。
创建数据源(数据源内的列名客户是可以赋值数据的)
DataSet dsRep = new DataSet();
DataTable dtSheet = preintDataSet.Tables[0].Copy();
dtSheet.TableName = "parent";
dsRep.Tables.Add(dtSheet);
DataTable dtDetail = preintDataSet.Tables[1].Copy();
dtDetail.TableName = "child";
dsRep.Tables.Add(dtDetail);
//设置主表和从表的父子关系
DataColumn parentColumn = dsRep.Tables["parent"].Columns["ID"];
DataColumn childColumn = dsRep.Tables["child"].Columns["ID"];
DataRelation R1 = new DataRelation("R1", parentColumn, childColumn);
dsRep.Relations.Add(R1);
XtraReport mReport = new XtraReport();
mReport.LoadLayout(Path.Combine(Directory.GetCurrentDirectory(), mblj)); //报表模板文件
//查找组件
GroupFooterBand GroupFooter1 = mReport.FindControl("GroupFooter1", true) as GroupFooterBand;
GroupHeaderBand GroupHeader1 = mReport.FindControl("GroupHeader", true) as GroupHeaderBand;
DetailReportBand DetailReport = mReport.FindControl("DetailReport", true) as DetailReportBand;
DetailBand detailBand = mReport.FindControl("Detail", true) as DetailBand;
DetailBand detail1 = mReport.FindControl("Detail1", true) as DetailBand;
//先绑定数据源
mReport.DataMember = "parent";
mReport.DataSource = dsRep;
DetailReport.DataMember = "R1";
DetailReport.DataSource = dsRep;
if (detailBand != null)
{
DataBindHeadControl(detailBand.Controls, dsRep);
}
else
{
MessageBox.Show("没有默认模板,请放入默认模板到根路径下面文件名(XtraReport1.repx)");
return;
}
if (detail1 != null)
{
DataBindBodyControl(detail1.Controls, dsRep);
}
else
{
MessageBox.Show("没有默认模板,请放入默认模板到根路径下面文件名(XtraReport1.repx)");
return;
}
//在页脚之后设置分页符
GroupFooter1.PageBreak = PageBreak.AfterBand;
if (System.IO.File.Exists(newPath2))
{
mReport.PrintingSystem.PageSettings.RestoreFromXml(newPath2);
}
mReport.CreateDocument();
mReport.PrintingSystem.ShowMarginsWarning = false;
mReport.PrintingSystem.ExportOptions.PrintPreview.DefaultExportFormat = PrintingSystemCommand.ExportXls;
mReport.ShowPreview();
mReport.PrintingSystem.PageSettings.SaveToXml(newPath2);
---------------------------------------------------------------------
private void DataBindBodyControl(XRControlCollection control, DataSet dsSet)
{
foreach (XRControl c in control)
{
if (c is XRPanel)
{
DataBindBodyControl(c.Controls, dsSet);
}
if (c is XRTableRow)
{
DataBindBodyControl(c.Controls, dsSet);
}
if (c is XRTable)
{
DataBindBodyControl(c.Controls, dsSet);
}
if (c is XRLabel)
{
XRLabel txt = c as XRLabel;
string tag = (string)txt.Tag;
if (!string.IsNullOrEmpty(tag))
{
if (dsSet.Tables[1].Columns.Contains(tag))
{
txt.DataBindings.Add("Text", dsSet, "R1." + tag);
}
}
}
if (c is XRTableCell)
{
XRTableCell txt = c as XRTableCell;
string tag = (string)txt.Tag;
if (!string.IsNullOrEmpty(tag))
{
if (dsSet.Tables[1].Columns.Contains(tag))
{
txt.DataBindings.Add("Text", dsSet, "R1." + tag);
}
}
}
if (c is XRPictureBox)
{
XRPictureBox txt = c as XRPictureBox;
txt.ImageUrl = newPathlogo;
}
if (c is XRBarCode)
{
XRBarCode txt = c as XRBarCode;
string tag = (string)txt.Tag;
if (!string.IsNullOrEmpty(tag))
{
if (dsSet.Tables[1].Columns.Contains(tag))
{
txt.DataBindings.Add("Text", dsSet, "R1." + tag);
}
}
}
}
}