[转]多个gridview怎么导出同一个Excel文档?

有三个gridview :A  B  C
B C 是根据 A查出来的 
现在想把它们都导出到一个EXCEL里面

   private void GridViewToExcel()
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        gvSelectorder.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
    }

这样写只能导出一个   如何才能全部导出 在一个excel里面?答案如下:

1、你把两个GridView放在一个div里,比如这样

1 <div runat="server" id="divExport">
2   <gridview1 .....>
3   <gridview2 .....>
4 </div>

2、然后后台 gvSelectorder.RenderControl(oHtmlTextWriter); 这句改成 divExport.RenderControl(oHtmlTextWriter); 试一下

 

 

posted @ 2013-06-11 08:23  Seasons1987  阅读(874)  评论(0编辑  收藏  举报