我想将数据从GridView导出到ASP.NET网站上的excel文件中。我为此只添加了一个GridView
<body>
<form id="mainForm" runat="server">
<asp:GridView ID="exportGrid" runat="server">
</asp:GridView>
</form>
....
</body>
在代码隐藏中我有这个:
public override void VerifyRenderingInServerForm(Control control) { }
var result = GetDataIQueryable(); //A method that returns an IQueryable
exportGrid.DataSource = result;
exportGrid.DataBind();
Response.Clear();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + "ExcelFile.xls");
Response.ContentType = "application/excel";
var sw = new System.IO.StringWriter();
var htw = new HtmlTextWriter(sw);
exportGrid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
问题是允许我在计算机上保存excel文件的弹出窗口没有出现。我应该在代码中更改什么?
答案 0 :(得分:1)
在按钮点击事件
上使用此代码 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 */
}
并在.aspx页面上使用
<%@ Page Title="" Language="C#" EnableEventValidation="false"%>
答案 1 :(得分:1)
我知道这有点太晚了,但我发布这个只是为了获取信息。我在评论中找到了答案,但我仍然希望用代码提交答案,因为我自己发现很难找到代码所以答案是针对你的网格是否在更新面板内。您需要在按钮上添加回发触发器。
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();
}
}
在页面加载时不要忘记添加:
protected void Page_Load(object sender, EventArgs e)
{
this.RegisterPostBackControl();
}