完成后的效果图
很早之前就做过了,但是忘记了。
网上的那个在我实际开发中用处不大,但是也可以借鉴。
http://www.cnblogs.com/Reeezak/archive/2006/07/09/446444.aspx
我自己的,在生成Excel尾部加入统计信息:
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "统计:";
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[1].Text = "Avg:";//获取统计信息
e.Row.Cells[1].ColumnSpan = 10;
for (int i = 2; i <= 10; i++)
{
e.Row.Cells[i].Visible = false;
}
}
}
如果在页面显示,加入分页,想在页尾显示,只需要在分页时加入最后页显示就可以了。
eg:myGridView.ShowFooter=(pds.IsLastPage ? false : true);
实际操作中就出现了问题,我用以下方法获取统计信息:
ArrayList list = new ArrayList();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (!list.Contains(dt.Rows[i][columnname]))
{
list.Add(dt.Rows[i][columnname]);
}
}
return list.Count;
我在Page_Load里执行此操作,可统计信息放在页尾RowDataBound里面,我定义了全局变量,可是无法得到数据.
研究发现,得不到数据很正常.后来这样做,如下:
private int TotalRowCount = 15;//设置每页显示的行数
private int numCount = 0;//自动填充的行数
private int hours1;//一般件
private int hours2;//急速件
private int hours3;//最速件
private int state1;//处理中
private int state2;//已结案
private int state3;//已退件
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.Header)
//{
// e.Row.Attributes.Add("style", "background-image:url('./HeadImages/headeImage.jpg')");
//}
int strid = e.Row.RowIndex;
//if (strid != -1)
//{
// e.Row.Cells[0].Text = (this.myGridView.PageIndex * this.myGridView.PageSize + strid + 1).ToString();
//}
HyperLink strHLPrint = (HyperLink)e.Row.FindControl("myHLPrints");
if (strHLPrint != null)
{
if (((HiddenField)e.Row.FindControl("hdend_mk")).Value != "1")
{
strHLPrint.Enabled = false;
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 计算自动填充的行数
numCount++;
switch (DataBinder.Eval(e.Row.DataItem, "hours").ToString())
{
case "1":
hours1++;
break;
case "2":
hours2++;
break;
case "3":
hours3++;
break;
}
string strtype = DataBinder.Eval(e.Row.DataItem, "end_mk").ToString();
if (strtype == "0")
{
state1++;
}
if (strtype == "1")
{
state2++;
}
if (strtype == "2" || strtype == "3" || strtype == "4" || strtype == "5")
{
state3++;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
// 计算完毕,在此添加缺少的行
int toLeft = TotalRowCount - numCount;
int numCols = myGridView.Rows[0].Cells.Count;
for (int i = 0; i < toLeft; i++)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
for (int j = 0; j < numCols; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
myGridView.Controls[0].Controls.AddAt(numCount + 1 + i, row);
}
e.Row.Cells[0].Text = "小计:";
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[1].Text = "一般件:<font color='red'>" + hours1 + "</font>; 急速件:<font color='red'>" + hours2 + "</font>; 最速件:<font color='red'>" + hours3 + "</font>; 处理中:<font color='red'>" + state1 + "</font>; 已结案:<font color='red'>" + state2 + "</font>; 已退件:<font color='red'>" + state3 + "</font>";//获取统计信息
e.Row.Cells[1].ColumnSpan = 11;
for (int i = 2; i <= 11; i++)
{
e.Row.Cells[i].Visible = false;
}
}
}