Asp.net 从GridView导出到Excel保存弹出窗口不工作
我想将数据从GridView导出到ASP.NET网站上的excel文件中。我添加GridView仅用于此目的Asp.net 从GridView导出到Excel保存弹出窗口不工作,asp.net,gridview,export-to-excel,Asp.net,Gridview,Export To Excel,我想将数据从GridView导出到ASP.NET网站上的excel文件中。我添加GridView仅用于此目的 <body> <form id="mainForm" runat="server"> <asp:GridView ID="exportGrid" runat="server"> </asp:GridView> </form> .... </body> 问题是,允许我在计算机上保存
<body>
<form id="mainForm" runat="server">
<asp:GridView ID="exportGrid" runat="server">
</asp:GridView>
</form>
....
</body>
问题是,允许我在计算机上保存excel文件的弹出窗口没有出现。我应该在代码中更改什么 在按钮单击事件中使用此代码
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FormReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
BindGridView();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
GridView1.Dispose();
}
}
#endregion
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protectedvoid btnExport\u单击(对象发送方,事件参数e)
{
Response.Clear();
Response.Buffer=true;
AddHeader(“内容处置”、“附件;文件名=FormReport.xls”);
响应。Charset=“”;
Response.ContentType=“application/vnd.ms excel”;
使用(StringWriter sw=new StringWriter())
{
HtmlTextWriter hw=新的HtmlTextWriter(sw);
//导出所有页面的步骤
GridView1.AllowPaging=false;
BindGridView();
GridView1.HeaderRow.BackColor=颜色.白色;
foreach(GridView1.HeaderRow.Cells中的TableCell单元格)
{
cell.BackColor=GridView1.HeaderStyle.BackColor;
}
foreach(GridView1.Rows中的GridViewRow行)
{
row.BackColor=Color.White;
foreach(行中的表格单元格。单元格)
{
如果(row.RowIndex%2==0)
{
cell.BackColor=GridView1.AlternatingRowStyle.BackColor;
}
其他的
{
cell.BackColor=GridView1.RowStyle.BackColor;
}
cell.CssClass=“textmode”;
}
}
GridView1.渲染控制(hw);
//将数字格式化为字符串的样式
字符串样式=@“.textmode{}”;
回应。写作(风格);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
GridView1.Dispose();
}
}
#端区
公共覆盖无效VerifyRenderingInServerForm(控件)
{
/*验证是否呈现控件*/
}
并在.aspx页面上使用
<%@ Page Title="" Language="C#" EnableEventValidation="false"%>
我知道这有点太晚了,但我发布这篇文章只是为了提供信息。我在评论中找到了答案,但我仍然希望提交一个带有代码的答案,因为我自己发现很难找到代码,所以答案是针对您的网格是否在更新面板中的问题。您需要在按钮上添加后向触发器
private void RegisterPostBackControl()
{
ScriptManager.GetCurrent(this).RegisterPostBackControl(yourButton);
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
您的按钮单击事件:
protected void yourButton_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "full", true);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FormReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
yourGrid.AllowPaging = false;
var emps = FunctionThatGetsGridValues();
yourGrid.DataSource = emps;
yourGrid.DataBind();
yourGrid.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in yourGrid.HeaderRow.Cells)
{
cell.BackColor = yourGrid.HeaderStyle.BackColor;
}
foreach (GridViewRow row in yourGrid.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = yourGrid.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = yourGrid.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
yourGrid.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
yourGrid.Dispose();
}
}
如果这个解决方案对你有效,别忘了把它当作同一个问题的答案。我没有收到任何错误,但是弹出窗口没有出现,因此我无法保存excel文件。您是否在导出按钮的单击事件中使用该代码。是在单击事件方法中。我在您的代码()中发现了这个问题,更正后的Response.End()仍然不起作用。是,设置为false。
protected void yourButton_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "full", true);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FormReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
yourGrid.AllowPaging = false;
var emps = FunctionThatGetsGridValues();
yourGrid.DataSource = emps;
yourGrid.DataBind();
yourGrid.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in yourGrid.HeaderRow.Cells)
{
cell.BackColor = yourGrid.HeaderStyle.BackColor;
}
foreach (GridViewRow row in yourGrid.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = yourGrid.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = yourGrid.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
yourGrid.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
yourGrid.Dispose();
}
}
protected void Page_Load(object sender, EventArgs e)
{
this.RegisterPostBackControl();
}