码迷,mamicode.com
首页 > 其他好文 > 详细

datagrid指定行合并导出

时间:2014-10-15 01:02:09      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   for   sp   on   代码   html   

导出代码:

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   io   os   ar   for   sp   on   代码   html   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!