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

(急.....)怎样将查询出来的数据导出为EXCEL文件???
如题.
我要先将查询出来的数据显示在Gridview控件中,然后导出为EXCEL文件.
查询出来的每一笔数据的字段不一样,我应该怎样实现上面的功能,谢谢!!!

------解决方案--------------------
public static void GenerateByGridView(string Typename, GridView TempGrid)
{
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8 ";
string Filename = Typename + ".xls ";
HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "online;filename= " + Filename);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8 ");
HttpContext.Current.Response.ContentType = "application/ms-excel ";
//this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
TempGrid.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End();
}

Typename 为文件名
------解决方案--------------------
up~~~~
------解决方案--------------------
请参考这篇文章:
http://dotnet.aspx.cc/article/700bd3fa-a17f-41dc-b258-0dc572625700/read.aspx