none
asp导出数据到excel RRS feed

  • 问题

  • 为什么我的GridView导出数据后连button和textbox也导出来了?请大侠们帮帮忙!!!!!!!!!!!
    2009年12月16日 14:19

答案

  • 你好,

    这个filename是你下载时保存文件的默认名字。
    之所以有button等东西,是因为你直接调用了RenderControl方法。
    你可以采用下面的代码试试。

        public void Export(string fileName, GridView gv)
        {
           Response.Clear();
           //Response.Charset = "GB2312";
    
           Response.ContentEncoding = System.Text.Encoding.UTF8;
    
           Response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
           Response.ContentType = "application/ms-excel"; //"application/ms-word"; 
    
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    this.GridView1.RenderControl(htw);
                    //  Create a table to contain the grid
                    Table table = new Table();
    
                    //  include the gridline settings
                    table.GridLines = gv.GridLines;
    
                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }
    
                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }
    
                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }
    
                    //  render the table into the htmlwriter
                    table.RenderControl(htw);
    
                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }
    
        /// <summary>
        /// Replace any of the contained controls with literals
        /// </summary>
        /// <param name="control"></param>
        private void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }
    
                if (current.HasControls())
                {
                   PrepareControlForExport(current);
                }
            }
        }
    }

    Microsoft Online Community Support
    • 已标记为答案 海格 2009年12月23日 13:40
    2009年12月22日 4:06

全部回复

  • 你不贴代码的话,只有你自己知道。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2009年12月16日 19:18
    版主
  • Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());我想是这里的问题,filename的意思是不是下载整个页面啊?那只要控件的值该怎么写啊?
    2009年12月21日 14:23
  • 你好,

    这个filename是你下载时保存文件的默认名字。
    之所以有button等东西,是因为你直接调用了RenderControl方法。
    你可以采用下面的代码试试。

        public void Export(string fileName, GridView gv)
        {
           Response.Clear();
           //Response.Charset = "GB2312";
    
           Response.ContentEncoding = System.Text.Encoding.UTF8;
    
           Response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
           Response.ContentType = "application/ms-excel"; //"application/ms-word"; 
    
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    this.GridView1.RenderControl(htw);
                    //  Create a table to contain the grid
                    Table table = new Table();
    
                    //  include the gridline settings
                    table.GridLines = gv.GridLines;
    
                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }
    
                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }
    
                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }
    
                    //  render the table into the htmlwriter
                    table.RenderControl(htw);
    
                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }
    
        /// <summary>
        /// Replace any of the contained controls with literals
        /// </summary>
        /// <param name="control"></param>
        private void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }
    
                if (current.HasControls())
                {
                   PrepareControlForExport(current);
                }
            }
        }
    }

    Microsoft Online Community Support
    • 已标记为答案 海格 2009年12月23日 13:40
    2009年12月22日 4:06