datalist导出excel

 void ExportExcel( System.Web.UI.WebControls.DataList dl, string strFileName)
        {
           
            strFileName
= System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);


         
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer
= true;
            System.Web.HttpContext.Current.Response.Charset
= "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader(
"Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding
= System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType
= "application/ms-excel";
          this.EnableViewState = false; 
            System.Globalization.CultureInfo myCItrad
= new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter
= new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter
= new System.Web.UI.HtmlTextWriter(oStringWriter);

            dl.RenderControl(oHtmlTextWriter);
          
            
            System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace(
"<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer
= false;
            System.Web.HttpContext.Current.Response.End();
        }

 

//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 
    
public override void VerifyRenderingInServerForm(Control control)
    {
    }

 

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

 CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

 

你可能感兴趣的:(datalist)