求高手们给一个在asp.net中将DataGrid中的数据导出到EXCEL中,小弟不胜感激。
最好不要那个一输入名字就弹出另存为的窗口的那个例子。那个效果一点都不好。
连ASP。NET的布局都导出了!!!

解决方案 »

  1.   

    你去codeproject里面搜索datagrid+excel
      

  2.   

    留下你的EMAIL地址,我回去后发段代码给你,很好用的,我现在在公司,公司里没有
      

  3.   

    [email protected]
    给我一份,谢谢!
      

  4.   

    一下代码演示点击按钮将datagrid的数据导出到excel,当然datagrid的数据就是数据库的private void Page_Load(object sender, System.EventArgs e)
    {
    DataSet objDataset = new DataSet();
    SqlConnection objConn = new SqlConnection();
    objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
    objConn.Open();
    SqlDataAdapter objAdapter = new SqlDataAdapter("Select top 5 * from customers where country='USA'",objConn);
    objAdapter.Fill(objDataset);
    DataView oView = new DataView(objDataset.Tables[0]);
    dgExcel.DataSource = oView;
    dgExcel.DataBind();
    objConn.Close();
    objConn.Dispose();
    objConn = null;
    if(Request.QueryString["bExcel"] == "1")
    {
    Response.ContentType = "application/vnd.ms-excel";Response.Charset = "";//关闭 ViewState
    EnableViewState = false;
    System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
    //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
    //获取control的HTML
    dgExcel.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
    // 把HTML写回浏览器
    Response.Write(tw.ToString());
    Response.End();
    }}#region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }/// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnGetExcel.Click += new System.EventHandler(this.btnGetExcel_Click);
    this.Load += new System.EventHandler(this.Page_Load);}
    #endregionprivate void btnGetExcel_Click(object sender, System.EventArgs e)
    {
    Response.Redirect("excel.aspx?bExcel=1");
    }
      

  5.   

    能给我发一份吗  
    [email protected]
    感谢 `` 感谢```
      

  6.   

    public static void ToExcel(System.Web.UI.Control ctl,string FileName)
            {
                HttpContext.Current.Response.Charset ="UTF-8";
                HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
                HttpContext.Current.Response.ContentType ="application/ms-excel";
                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls");
                ctl.Page.EnableViewState =false;
                System.IO.StringWriter  tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                ctl.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }        
      

  7.   

    晕你们发的不都是点击按钮。然后就弹出一个保存的对话框。然后就保存。但你们有没有试过用SQL管理器导入刚刚保存的那个XLS,看会出现什么问题
      

  8.   

    HttpContext.Current.Response.ContentType ="application/ms-excel";
    还有一个问题。这条语句后面能不能指定类型的。
    比如是97-2000,5.0之类的类型。