日期:2014-05-18  浏览次数:20450 次

请问一个关于DataGrid数据导出到Excel的问题
我的代码在2003及以下可以导出,

如果在XP运行的话,会直接在浏览器打开,不能导出,请问是甚么原因啊!!!

------解决方案--------------------
帮你up
------解决方案--------------------
给你个例子参考一下
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader( "content-disposition ", "attachment;filename=FileName.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);

gvMain.AllowPaging = false;
//BindData();
InKu ikMain = new InKu();
gvMain.BackColor = System.Drawing.Color.White;
gvMain.ForeColor = System.Drawing.Color.Black;
gvMain.BorderWidth = 1;
gvMain.RowStyle.BackColor = System.Drawing.Color.White;
gvMain.RowStyle.ForeColor = System.Drawing.Color.Black;
gvMain.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
gvMain.BorderStyle = BorderStyle.Solid;
gvMain.BorderWidth = 1;
gvMain.AlternatingRowStyle.ForeColor = System.Drawing.Color.Black;

ikMain.GridViewListBind(gvMain, QueryString(), 0);

gvMain.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();


gvMain.AllowPaging = true;
//BindData();
ikMain.GridViewListBind(gvMain, QueryString(), 0);
}