上一篇主要是说了activereport的配置和一个简单的示例,这次我来详细说一下,如何生成一个动态的报表。
报表分为三块,pageHeader,pageFooter,还有detail,
pageHeader是设置列名和title,detail是设计表的内容包括统计数据,pageFooter设置页脚。
第一步,我们新建一个报表文件,一个是设计页面,一个是源代码的页面,我们可以在设计页面进行布局,不过我是针对我的项目而言,事先不知道要显示多少列,所以我直接写的代码。
首先:我重载了构造函数,加了两个参数,因为我要获得我要显示多少列,还有数据查询语句的条件,具体代码如下:
public AssetInfoReport(string filterSql,string columnName)
{
//
// Required for Windows Form Designer support
//
this.filterSql = filterSql;
//这是我把要显示的列放在一个字符串中了,其实仁者见仁,有好多种方法可以使用,可以根据具体情况做改动。
this.columnNameArr = columnName.Split(';');
InitializeComponent();
}
然后我们就可以在 InitializeComponent这个方法中进行我的设计了。
第二步 参数设计
//设置打印的方向,Landscape为横向,Portrait为纵向
this.PageSettings.Orientation = DataDynamics.ActiveReports.Document.PageOrientation.Landscape;
//设置打印的纸张大小,我设的为A4.
this.PageSettings.PaperHeight = 11.7F;
this.PageSettings.PaperWidth = 8.267716F;
this.PageSettings.Margins.Top = 1;
this.PageSettings.Margins.Bottom = 1;
this.PageSettings.Margins.Left = 1;
this.PageSettings.Margins.Right = 1;
//设置实际打印的宽度,
this.PrintWidth = this.PageSettings.PaperHeight - this.PageSettings.Margins.Left - this.PageSettings.Margins.Right;
第三步,报表设计
if (columnNameArr != null)
{
float _top = 0.7F;
float _left = 0F;
float _width = this.PrintWidth / columnNameArr.Length;
for (int i = 0; i < columnNameArr.Length; i++)
{
string labelID = "lbl" + columnNameArr[i].Split(',')[0];
string textBoxID = "txt" + columnNameArr[i].Split(',')[0];
string lblValue = columnNameArr[i].Split(',')[1];
string textBoxValue = columnNameArr[i].Split(',')[0];
//初始化Label background-color: #C7E0FA;overflow-y:hidden
Label lbl = new DataDynamics.ActiveReports.Label();
lbl.Name = labelID;
lbl.Value = lblValue;
lbl.Text = lblValue;
lbl.Border.BottomColor = System.Drawing.Color.Black;
lbl.Border.BottomStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
lbl.Border.LeftColor = System.Drawing.Color.Black;
lbl.Border.LeftStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
if (i == columnNameArr.Length - 1)
{
lbl.Border.RightColor = System.Drawing.Color.Black;
lbl.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
}
else
{
lbl.Border.RightColor = System.Drawing.Color.Black;
lbl.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
}
lbl.Border.TopColor = System.Drawing.Color.Black;
lbl.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
lbl.Style = "text-align: center; font-size: 10pt; vertical-align: middle;font-weight:bold";
lbl.Height = 0.3F;
lbl.Left = _left;
lbl.Top = _top;
lbl.Width = _width;
//将Label添加到报表头中
this.pageHeader.Controls.Add(lbl);
//初始化TextBox
TextBox txt = new DataDynamics.ActiveReports.TextBox();
txt.Name = textBoxID;
txt.DataField = textBoxValue;
txt.Alignment = DataDynamics.ActiveReports.TextAlignment.Center;
txt.Border.BottomStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
txt.Border.LeftStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
if (i == columnNameArr.Length - 1)
{
txt.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
//txt.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
}
else
{
txt.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
txt.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
}
txt.CanGrow = false;
txt.DistinctField = null;
txt.Left = _left;
txt.Style = "font-size: 10pt;text-align: center;";
txt.VerticalAlignment = DataDynamics.ActiveReports.VerticalTextAlignment.Middle;
txt.Width = _width;
txt.Size = new System.Drawing.SizeF(_width, 0.3F);
//将TextBox添加到报表体中
this.detail.Controls.Add(txt);
_left += lbl.Width;
}
}
当然还有pageHeader,pageFooter,detail的高度等参数的设置,这里就不再多说.
第四步 数据源绑定
this.DataSource = new AssetsReportInfo().GetAssetInfo(filterSql);
然后再新建一个页面,在新页面中调用此报表,具体参见上一篇文章.这样一个动态报表打印就完成了.
当然里面还有很多细节上面的东东,其实我是想写成一个完全通用的报表,但是因为项目要求的很紧迫,感觉时间不够,以后我会逐渐完善的.