首页 > 其他 > 详细

datagrid指定行合并导出

时间:2014-10-14 23:36:40      阅读:423      评论:0      收藏:0      [点我收藏+]

导出代码:

public void GridViewToExcel(GridView ctrl, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.AllowPaging = false;
bind();
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
ctrl.AllowPaging = true;
bind();
}

重写方法,此方法必须需要:

public override void VerifyRenderingInServerForm(Control control)
{

}

指定合并列:

protected void GridView1_DataBound(object sender, EventArgs e)
{
    int[] arr = new int[] { 1,3,5 };
    GroupRows(GridView1, arr, 2);
    GroupRows(GridView1, arr, 0);
    GroupRows(GridView1, arr, 1);
}

 

//合并
public static void GroupRows(GridView GridView1, int[] cellIndex, int mostlyid)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
     GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
      GridViewRow gvrNext = GridView1.Rows[i];
if (gvr.Cells[cellIndex[mostlyid]].Text == gvrNext.Cells[cellIndex[mostlyid]].Text)// && gvr.Cells[cellIndex[mostlyid]].Text == gvrNext.Cells[cellIndex[mostlyid]].Text)
{
      gvrNext.Cells[cellIndex[mostlyid]].Visible = false;//不然会把其他的挤走,造成行突出
      rowSpanNum++;
}
else
{
      gvr.Cells[cellIndex[mostlyid]].RowSpan = rowSpanNum;
      rowSpanNum = 1;
      break;
}
if (i == GridView1.Rows.Count - 1)
{
      gvr.Cells[cellIndex[mostlyid]].RowSpan = rowSpanNum;
}
}
}
}

datagrid指定行合并导出

原文:http://www.cnblogs.com/luozenghui/p/4025368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!