GridView数据导出功能

it2022-05-09  30

导出成xls文件用excel打开。通用的代码如下:

 

  #region  导出          // common          public   void  ExcuteOut(GridView gv)        {             if  (gv.Rows.Count  >   0 )            {                Response.ClearContent();                Response.AddHeader( " content-disposition " " attachment; filename=DMReport "   +  DateTime.Now.ToString( " _yyyyMMdd_HHmmss " +   " .xls " );                Response.ContentEncoding  =  System.Text.Encoding.UTF8;                Response.ContentType  =   " application/excel " ;                StringWriter sw  =   new  StringWriter();                HtmlTextWriter htw  =   new  HtmlTextWriter(sw);                gv.RenderControl(htw);                Response.Write(sw.ToString());                Response.End();            }             else             {                Tools.Alert(Page,  " 没有数据! " );            }        }         public   override   void  VerifyRenderingInServerForm(Control control)        { }         #endregion

 

 必须加上后面的VerifyRenderingInServerForm函数,否则会报错。

注意要使用utf8编码,不能用default。

转载于:https://www.cnblogs.com/tuyile006/archive/2009/05/08/1452498.html

相关资源:数据结构—成绩单生成器

最新回复(0)