日期:2014-05-20  浏览次数:20435 次

从GridView导出数据到Excel中时如何不导出删除列
我在从GridView导出数据到Excel中时把删除列和编辑列都导出来了,如果不想导出那两列怎么控制?最好给出源码,谢谢

------解决方案--------------------
//导出时注意把删除与编辑列设置为不可见就行了
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);

//这里的3代表是删除列所在列
this.GridView1.Columns[3].Visible = false;

this.GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

//导出完毕重新设置可见
this.GridView1.Columns[3].Visible = false;