在水晶报表中动态的添加自己所需要的的字段
由于常遇到一些比较大的报表,其中的字段有的需要,有的不需要,有的这次需要,下次可能就不需要,不可能把所有的可能性都考虑在内做报表,所以就需要动态的添加字段,以实现动态报表。
做动态报表主要用的是水晶报表中的自定义公式,首先添加N个自定义公式在报表中(其中N为字段最多可能的数),并且在设计报表中要设置好线与线之间的距离,还有文本对象的宽度,自定义公式的宽度。如图:
//首先实例化一个报表(zkck)cr1
reports.zkck cr1=new gjmis.wzsys.reports.zkck ();
conn=new SqlConnection (this.strcon );
sqldap=new SqlDataAdapter ();
selcmd=new SqlCommand();
sqldap.SelectCommand=selcmd;
selcmd.CommandText ="[zkckbd]";
//调用存储过程
selcmd.CommandType=CommandType.StoredProcedure;
selcmd.Connection =conn;
//接收通过网页传递过来的参数(即要在报表上要显示的字段)
string []field=Regex.Unescape((Request["strcheck"])).Split(',');
selcmd.Parameters.Add("@imonth",Convert.ToInt32 (Request["imonth"]));
sqldap.Fill (sqldst,"zkck");
//实例化报表中的文本对象
TextObject[] txt=new TextObject[17];
TextObject[] txt_1=new TextObject [17];
TextObject[] txt_2=new TextObject [17];
//实例化报表中的自定义公式
FormulaFieldDefinition[] field_1=new FormulaFieldDefinition[17];
FormulaFieldDefinition[] field_2=new FormulaFieldDefinition[17];
//实例化报表中的自定义公式(对详细资料中的字段进行汇总)
FormulaFieldDefinition[] tot_1=new FormulaFieldDefinition[18];
FormulaFieldDefinition[] tot_2=new FormulaFieldDefinition[18];
for(int i=1;i<=17;i++)
{
//为实例化的报表文本与报表中的文本对象相对应
txt[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt"+Convert.ToString (i)];
txt_1[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt_1"+Convert.ToString (i)];
txt_2[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt_2"+Convert.ToString (i)];
//为实例化的自定义公式与报表中的自定义公式相对应
field_1[i-1]=cr1.DataDefinition.FormulaFields["field_1"+Convert.ToString (i)];
field_2[i-1]=cr1.DataDefinition.FormulaFields["field_2"+Convert.ToString (i)];
tot_1[i-1]=cr1.DataDefinition.FormulaFields["tot_1"+Convert.ToString (i)];
tot_2[i-1]=cr1.DataDefinition.FormulaFields["tot_2"+Convert.ToString (i)];
//初始设置所有报表中的所有对象为不显示
cr1.ReportDefinition.ReportObjects["tot1"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
cr1.ReportDefinition.ReportObjects["tot2"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
cr1.ReportDefinition.ReportObjects["field1"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
cr1.ReportDefinition.ReportObjects["field2"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
cr1.ReportDefinition.ReportObjects["ln1"+Convert.ToString(i)].ObjectFormat .EnableSuppress =true;
cr1.ReportDefinition.ReportObjects["ln2"+Convert.ToString(i)].ObjectFormat .EnableSuppress =true;
}
for(int i=0;i<field.Length-1;i++)
{
//为要显示的自定义的文本对象赋值(即同时给报表中的文本对象赋值)
txt[i].Text=field[i].ToString();
txt_1[i].Text="张数";
txt_2[i].Text="金额";
//为要显示的自定义的公式赋值(即同时给报表中的自定义公式赋值)
field_1[i].Text="{zkckbd."+field[i] +"张数}";
tot_1[i].Text="sum({zkckbd."+field[i] +"张数})";
field_2[i].Text="{zkckbd."+field[i] +"}";
tot_2[i].Text="sum({zkckbd."+field[i] +"})";
//赋值过的对象(要在报表中显示的字段)让它们显示
cr1.ReportDefinition.ReportObjects["field1"+Convert.ToString(i+1)+"1"].ObjectFormat.EnableSuppress=false;
cr1.ReportDefinition.ReportObjects["field2"+Convert.ToString(i+1)+"1"].ObjectFormat.EnableSuppress=false;
cr1.ReportDefinition.ReportObjects["tot1"+Convert.ToString (i+1)+"1"].ObjectFormat .EnableSuppress = false;
cr1.ReportDefinition.ReportObjects["tot2"+Convert.ToString (i+1)+"1"].ObjectFormat .EnableSuppress = false;
cr1.ReportDefinition.ReportObjects["ln1"+Convert.ToString(i+1)].ObjectFormat.EnableSuppress =false;
cr1.ReportDefinition.ReportObjects["ln2"+Convert.ToString(i+1)].ObjectFormat.EnableSuppress =false;
}
tot_1[17]=cr1.DataDefinition.FormulaFields["tot_118"];
tot_2[17]=cr1.DataDefinition.FormulaFields["tot_218"];
//
tot_1[17].Text ="sum({zkckbd.张数})";
tot_2[17].Text ="sum({zkckbd.合计})";
//对一些始终显示的文本的位置进行定位
// cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot1181"].ObjectFormat.EnableSuppress =false;
// cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot2181"].ObjectFormat.EnableSuppress =false;
cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot1181"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["tot2181"].Left=cr1.ReportDefinition.ReportObjects["tot2181"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["fieldzs1"].Left=cr1.ReportDefinition.ReportObjects["fieldzs1"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["fieldje1"].Left=cr1.ReportDefinition.ReportObjects["fieldje1"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["txt_hj"].Left=cr1.ReportDefinition.ReportObjects["txt_hj"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["txt_je"].Left=cr1.ReportDefinition.ReportObjects["txt_je"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["txt_zs"].Left=cr1.ReportDefinition.ReportObjects["txt_zs"].Left-(18-field.Length)*1590;
cr1.ReportDefinition.ReportObjects["txtzbr"].Left=cr1.ReportDefinition.ReportObjects["txt_zs"].Left ; cr1.ReportDefinition.ReportObjects["txt_title"].Width=1845+cr1.ReportDefinition.ReportObjects["txt1"].Width*(field.Length);
//文本及字段的移动成功(下面是对一些线作出必要的调整)
lbltmp.Text="<script>var bl;var kd; var zs;var je;var t_kd; bl=parseInt(ln0.style.left);kd=1590*bl/1845;t_kd=1740*bl/1845;zs=470*bl/1845; je=1120*bl/1845; ln_hj1.style.left=bl+zs+"+(field.Length-1)+"*kd;ln_hj2.style.left=bl+"+(field.Length)+"*kd;lntop1.style.width=t_kd+"+(field.Length)+"*kd;lntop2.style.width="+(field.Length)+"*kd;lntop3.style.width=t_kd+"+(field.Length)+"*kd;lntop5.style.width=t_kd+"+(field.Length)+"*kd;for(var i=0;i<"+sqldst.Tables[0].Rows.Count+";i++)lntop4[i].style.width=t_kd+"+(field.Length)+"*kd;</script>";
cr1.SetDataSource(this.sqldst.Tables["zkck"]);
CrystalReportViewer1.ReportSource =cr1;
}