在asp.net中导出excel问题 [英] Export excel problem in asp.net
问题描述
我在Excel工作表上收到此错误..
System.InvalidOperationException:只能在Render()期间调用RegisterForEventValidation;
这里我的代码
protected void btnexport_Click(object sender,ImageClickEventArgs e)
{
尝试
{
Response.ClearContent();
Response.Buffer = true ;
Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},Planwise Report.xls));
Response.ContentType =application / ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
btnview_Click(this,null);
GridView1.AllowPaging = false;
//将标题行改回白色
Gri dView1.HeaderRow.Style.Add(background-color,#FFFFFF);
//将stlye应用于gridview标题单元格
for(int i = 0 ;我< GridView1.HeaderRow.Cells.Count; i ++)
{
GridView1.HeaderRow.Cells [i] .Style.Add(background-color,#507CD1);
}
int j = 1;
//此循环用于根据特定行将stlye应用于单元格
foreach(GridViewRow) GridView1.Rows中的gvrow)
{
gvrow.BackColor = Color.White;
if(j< = GridView1.Rows.Count )
{
if(j%2!= 0)
{
for(int k = 0 ; k< gvrow.Cells.Count; k ++)
{
gvrow.Cells [k] .Style.Add(background-color,#EFF3FB );
}
}
}
j ++;
}
GridVi ew1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch(例外情况)
{
Response.Write(ex .ToString());
}
终于
{
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/ *验证控件是否呈现* /
}
我的错误是什么..?
i am getting this error on excel sheet..
System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
here my code
protected void btnexport_Click(object sender, ImageClickEventArgs e)
{
try
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Planwise Report.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
btnview_Click(this, null);
GridView1.AllowPaging = false;
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in GridView1.Rows)
{
gvrow.BackColor = Color.White;
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
whats my mistake..?
推荐答案
类似问题的答案前一天
[ ^ ]
编辑
------------------- ------
Similar question with answer one day ago
Export excel problem in asp.net[^]
EDIT
-------------------------
但它的导出仅在页面上有什么..没有整个网格导出例如。我有500条记录,即时通讯使用分页,每页50条。仅输出50条记录。任何解决方案?先生..
but its exporting only whats on the page..its not exporting all over Grid eg. i have 500 records and im using paging like 50 per page .its exporting only 50 records .any solutions ?sir..
检查这个
导出GridView(所有页面),启用Paging(AllowPaging)到ASP中的Excel。净 [ ^ ]
这篇关于在asp.net中导出excel问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!