Gridview导出EXCEL问题
在Win2003上,IE是6.0 SP1,安装了Office2003,
从网页面上将GridView导出成EXCEL时,弹出下载页面都很正常,但只要你一点打开 / 保存 /取消 打开导出EXCEL的那个母页面就会自动关闭了,不知是怎么回事,在导出的脚本中没有一句是说要关闭IE窗口的。
------解决方案--------------------母页加上 <form id= "form1 " runat= "server " target= "_blank "> 这样试试。
没试过。你看看行不行
------解决方案--------------------GridView数据导入Excel
页面增加一个按钮,单击事件添加如下方法:
protected void Button1_Click(object sender, EventArgs e)
{
Export( "application/ms-excel ", "xxxx.xls ");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312 ";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
------解决方案-------------------- http://www.51aspx.com/S/Excel_GridView.aspx
http://www.51aspx.com/CV/GridViewDemo
------解决方案--------------------public partial class GWExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
SqlConnection con = new SqlConnection( "server=.;database=sample4;uid=sa;pwd=sa ");
SqlDataAdapter sda = new SqlDataAdapter( "select * from bb ",con);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
protected void paging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader( "content-disposition ", "attachment;filename=dfsgdfg.xls ");
Response.Charset = "gb2312 ";
Response.ContentType = "application/vnd.xls ";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.AllowPaging = false;
BindData();
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridView1.AllowPaging = true;
BindData();
}
}
------解决方案--------------------//如果没有下面方法会报错 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)