asp.net 中gridview导出到excel问题,要导出gridview,为什么每次都把整页倒出来了
代码:protectedvoidButton3_Click(objectsender,EventArgse){if(GridView1.Rows.Count>0){Res...
代码:
protected void Button3_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count>0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
这段都是网上找的。。。为什么导出的时候excel中都是导出整个页面啊? 展开
protected void Button3_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count>0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
这段都是网上找的。。。为什么导出的时候excel中都是导出整个页面啊? 展开
1个回答
展开全部
用我这段,我一直在用
protected void Button8_Click(object sender, EventArgs e)
{
GridView2.Visible = true;
string filename = "QueryData";
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/vnd.ms-excel; charset=UTF-8";
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView2.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
追问
下载的时候提示有3kb 还挺高兴的。。可是载完就成17kb了 还是整个页面啊。。。。好像excel中什么都有,就是没有gridview。。。跟原来的一样。。到底什么情况啊。。
追答
你还是看看你的GridView是不是OK的,你把GridView的html代码贴出来看看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询