datagrid指定行合并导出_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > datagrid指定行合并导出

datagrid指定行合并导出

 2014/10/15 3:19:59  luozenghui  程序员俱乐部  我要评论(0)
  • 摘要:导出代码:publicvoidGridViewToExcel(GridViewctrl,stringFileType,stringFileName){HttpContext.Current.Response.Charset="GB2312";HttpContext.Current.Response.ContentEncoding=Encoding.UTF8;HttpContext.Current.Response.AppendHeader("Content-Disposition"
  • 标签:tag

导出代码:

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;
}
}
}
}

发表评论
用户名: 匿名