关于分页后的datagrid数据导入到excel
请教各位高手一个问题,我的datagrid控件是分过页的,如何将分页后的所有数据一次性导出到excel呢,我在网上查到的都是一页一页的导出...
试过几个方法,比如1 在导出时候先将datagrid的allowpage属性设置为false,2 在导出时将datagrid的pagesize设置为很大...但是都不行.
请大家帮我想一下,谢谢!
------解决方案--------------------我是这么做的,再写个方法获取全部数据.然后把这个DataTable导出到EXCEL
------解决方案--------------------如:
public DataTable GetAllData()
{
sql = "select * from tb "
DataTable dt = ...;
return dt ;
}
------解决方案--------------------我也如楼上那样解决的 从取一下数据 然后导出
或者你把 分页=FALSE 然后把gridview 导出的Excel
------解决方案-------------------- private void btExport_Click(object sender, System.EventArgs e)
{ //有分页时导出
Common cm=new Common();
MyDataGrid.AllowPaging = false;
Bind();
cm.ToExcel(MyDataGrid);
MyDataGrid.AllowPaging = true;
Bind();
}
Commmon 类:using System;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;
namespace CMSystem.Include
{
/// <summary>
/// Common 的摘要说明。
/// </summary>
public class Common
{
public Common()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void ToExcel(System.Web.UI.Control ctl) //无分页导出
{
//System.Web.UI.WebControls.DataGrid dgExport = null;
//dgExport.AllowPaging = false;
HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "attachment;filename=Excel.xls ");
HttpContext.Current.Response.Charset = "UTF-8 ";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel ";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
//dgExport = new System.Web.UI.WebControls.DataGrid();
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
}
}