DataGrid输出到Excel可以实现。但是如果DataGrid有分页的话要如何实现呢?

解决方案 »

  1.   

    建议你去http://www.cnblogs.com/birdshome/favorite/1666.html
    这里前面的一篇文章不错,我就是用他的代码来做的
      

  2.   

    protected void lbExport_Click(object sender, EventArgs e)
        {
            ExportToExcel(dgExport);
        }
        
        private void ExportToExcel(Control ctrl)
        {
            Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel";        StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);        ctrl.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
      

  3.   

    doitnow2000(大海) 
    这个代码,如果分页的话会出错吧
      

  4.   

    to wkxgyg:
    数据源输出是最好的,而且我上面不是提供了方法页面么?
      

  5.   

    jimu8130(好累好累) 
    我看到了,里面好多个啊,具体是哪个?
      

  6.   

    to wkxgyg:就是第一个啊?他连源代码都公布了
      

  7.   

    to wkxgyg:上面不是有很多相关的导出excel的文章,你仔细看看