【发布时间】:2013-09-23 02:53:37
【问题描述】:
我正在使用将 Gridview 导出到 Excel。在 Gridview 出现之前,用户将进行一些过滤。我猜我的导出按钮似乎不起作用,因为我没有将 Gridview 放在页面加载中......
这是我的导出代码:
protected void Button2_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment; filename=logbook.xls");
Response.ContentType = "application/excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
我的 Gridview 代码:
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGridviewData(ddlSystemList.SelectedValue);
}
private void BindGridviewData(string s)
{
string ConnectionStringB = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
using (SqlConnection connectionB = new SqlConnection(ConnectionStringB))
{
connectionB.Open();
SqlCommand cmd = new SqlCommand();
if (s == "Webloan")
{
cmd.Connection = connectionB;
cmd.CommandText = "Select a.ldatetime as DATE, a.username as USERNAME, a.fullname as FULLNAME, a.description as DESCRIPTION, b.action_name as ACTION from WEBLOAN_SERVER.webloan.dbo.logbook a join WEBLOAN_SERVER.webloan.dbo.action_def b on a.action_no = b.action_no where DATEDIFF(day,ldatetime,@date_exec1) <= 0 and DATEDIFF(day,ldatetime,@date_exec2) >= 0";
cmd.CommandType = CommandType.Text;
}
if (s == "Saveplus")
{
cmd.Connection = connectionB;
cmd.CommandText = "Select top 10 from WEBLOAN_SERVER.saveplus.dbo.logbook where DATEDIFF(day,ldatetime,@date_exec) = 0";
cmd.CommandType = CommandType.Text;
}
cmd.Parameters.AddWithValue("@date_exec1", txtDate.Text);
cmd.Parameters.AddWithValue("@date_exec2", txtDate2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
connectionB.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex; //assign new page number
BindGridviewData(ddlSystemList.SelectedValue);
//GridView1.DataBind(); // bind data to gridview
}
我的页面加载代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtDate.Text = DateTime.Now.ToString("MM/d/yyyy");
txtDate2.Text = DateTime.Now.ToString("MM/d/yyyy");
}
}
我还添加了我的覆盖代码:
public override void VerifyRenderingInServerForm(Control control)
{
}
我是 ASP 新手...我非常需要这方面的帮助...谢谢
PS:我正在使用更新面板,而我的“生成到 Excel”按钮位于我的更新面板中
【问题讨论】:
-
你遇到了什么错误..?
-
@SANDEEP 我没有收到任何错误,它只是刷新了页面并且没有下载任何 excel 文件
-
检查它不是因为更新面板。你使用更新面板吗?如果你正在使用,那么使用触发器来生成 excel
-
@SANDEEP 我添加了一个触发器来控制我的导出按钮,我还尝试将我的按钮转移到更新面板之外......但是这两个选项仍然不起作用:(
标签: asp.net excel gridview export