使用GridView启动分页后如何导出EXCEL表


使用GridView启动分页后怎么导出EXCEL表
使用GridView启动分页后怎么导出EXCEL表
提示为:
只能在执行   Render()   的过程中调用   RegisterForEventValidation;

.net 程序开发 asp

好吃的白薯 12 years, 6 months ago



mjsteps answered 12 years, 6 months ago


还是从SQL导出比较方便!

栐恆D噓箜 answered 12 years, 6 months ago


那把全部的数据取道然后导出excel行了 在后台做

Diane-明 answered 12 years, 6 months ago


你想怎样个分页?

Eandic answered 12 years, 6 months ago


那我这样取得 从GridView取得DataTable,或者从SqlDataSource中


再从数据库里查一次

嗶哩嗶哩l answered 12 years, 6 months ago


把数据集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);
}


老衲法号汤姆 answered 12 years, 6 months ago


GridView的ID.AllowPaging = false;

RYTHEM answered 12 years, 6 months ago


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

米饭1024 answered 12 years, 6 months ago

Your Answer