当前位置: 代码迷 >> ASP.NET >> (急)怎样将查询出来的数据导出为EXCEL文件?解决方案
  详细解决方案

(急)怎样将查询出来的数据导出为EXCEL文件?解决方案

热度:6658   发布时间:2013-02-25 00:00:00.0
(急.....)怎样将查询出来的数据导出为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
  相关解决方案