GridView数据导出功能

导出成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。

posted @ 2009-05-08 11:39  小y  阅读(983)  评论(1编辑  收藏  举报