导出代码:

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

05-11 13:05