使用GridView启动分页后怎么导出EXCEL表?
使用GridView启动分页后怎么导出EXCEL表?
提示为:
只能在执行 Render() 的过程中调用 RegisterForEventValidation;
------解决方案--------------------private void btnMIME_Click(object sender, System.EventArgs e)
{
BindData();
Response.ContentType = "application/vnd.ms-excel ";
Response.AddHeader( "Content-Disposition ", "inline;filename= "
+ HttpUtility.UrlEncode( "下载文件.xls ",Encoding.UTF8 ) );
//如果输出为Word,修改为以下代码
//Response.ContentType = "application/ms-word "
//Response.AddHeader( "Content-Disposition ", "inline;filename=test.doc ")
StringBuilder sb=new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append( " <html> <body> ");
dgShow.RenderControl(hw);
sb.Append( " </body> </html> ");
Response.Write(sb.ToString());
Response.End();
}
如果有分页,目前知道的方法只有去掉分页,用RenderControl方法导出为Excel
------解决方案--------------------把数据集DataTable 导到Excel里
别人写的:
using OWC;
using System.Data.OleDb;
using System.IO;
SpreadsheetClass xlsheet=new SpreadsheetClass();
conn.Open();
cmd=new OleDbCommand(sql,conn);
OleDbDataReader reader=cmd.ExecuteReader();
int numbercols=reader.FieldCount;
int row=2;
int i=0;
//输出标题
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[1, i + 1] = reader.GetName(i).ToString();
}
// 输出字段内容
while(reader.Read())
{
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[row, i + 1] = reader.GetValue(i).ToString();
}
row = row + 1;
}
reader.Close();
conn.Close();
try
{
xlsheet.ActiveSheet.Export(Server.MapPath( ". ") + "\\mfExcel\\ " + this.xlfile.Text+ ".xls ",OWC.SheetExportActionEnum.ssExportActionNone);
}
catch(System.Runtime.InteropServices.COMException e )
{
Response.Write( "错误: " + e.Message);
}
------解决方案--------------------http://blog.csdn.net/blackhero/archive/2006/08/25/1116399.aspx
------解决方案--------------------GridView的ID.AllowPaging = false;
------解决方案--------------------那我这样取得 从GridView取得DataTable,或者从SqlDataSource中?
再从数据库里查一次
------解决方案--------------------你想怎样个分页?
------解决方案--------------------那把全部的数据取道然后导出excel行了 在后台做
------解决方案--------------------还是从SQL导出比较方便!