ASP.Net 2.0: Export GridView to Excel

using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  System.IO;
using  System.Reflection;
public   partial   class  KB : System.Web.UI.Page
{
    Hashtable htControls 
=   new  Hashtable();
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        htControls.Add(
" LinkButton " " Text " );
        htControls.Add(
" HyperLink " " Text " );
        htControls.Add(
" DropDownList " " SelectedItem " );
        htControls.Add(
" CheckBox " " Checked " );
        htControls.Add(
" TextBox " " Text " );
        htControls.Add(
" Button " "" );
    }
    
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        PrepareGridViewForExport(GridView1);
        ExportGridView();
    }
    
private   void  ExportGridView()
    {
        
string  attachment  =   " attachment; filename=Contacts.xls " ;
        Response.ClearContent();
        Response.AddHeader(
" content-disposition " , attachment);
        Response.ContentType 
=   " application/ms-excel " ;
        StringWriter sw 
=   new  StringWriter();
        HtmlTextWriter htw 
=   new  HtmlTextWriter(sw);
        HtmlForm hf 
=   new  HtmlForm();
        GridView1.Parent.Controls.Add(hf);
        hf.Attributes.Add(
" runat " " server " );
        hf.Controls.Add(GridView1);
        hf.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }


    
private   void  PrepareGridViewForExport(Control gv)
    {
        Literal l 
=   new  Literal();
        
for  ( int  i  =   0 ; i  <  gv.Controls.Count; i ++ )
        {
            
if  (( null   !=  htControls[gv.Controls[i].GetType().Name])  ||  
                (
null   !=  htControls[gv.Controls[i].GetType().BaseType.Name]))
            {
                l.Text 
=  GetControlPropertyValue(gv.Controls[i]);
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            
if  (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
    
private   string  GetControlPropertyValue(Control control)
    {
        Type controlType 
=  control.GetType();
        
string  strControlType  =  controlType.Name;
        
string  strReturn  =   " Error " ;
        
bool  bReturn;
        PropertyInfo[] ctrlProps 
=  controlType.GetProperties();

        
string  ExcelPropertyName  =  ( string )htControls[strControlType];

        
if  (ExcelPropertyName  ==   null )
        {
            ExcelPropertyName 
=  ( string )htControls[control.GetType().BaseType.Name];
            
if  (ExcelPropertyName  ==   null )
                
return  strReturn;
        }
        
foreach  (PropertyInfo ctrlProp  in  ctrlProps)
        {
            
if  (ctrlProp.Name  ==  ExcelPropertyName  &&
            ctrlProp.PropertyType 
==   typeof (String))
            {
                
try
                {
                    strReturn 
=  ( string )ctrlProp.GetValue(control,  null );
                    
break ;
                }
                
catch
                {
                    strReturn 
=   "" ;
                }
            }
            
if  (ctrlProp.Name  ==  ExcelPropertyName  &&
            ctrlProp.PropertyType 
==   typeof ( bool ))
            {
                
try
                {
                    bReturn 
=  ( bool )ctrlProp.GetValue(control,  null );
                    strReturn 
=  bReturn  ?   " True "  :  " False " ;
                    
break ;
                }
                
catch
                {
                    strReturn 
=   " Error " ;
                }
            }
            
if  (ctrlProp.Name  ==  ExcelPropertyName  &&
            ctrlProp.PropertyType 
==   typeof (ListItem))
            {
                
try
                {
                    strReturn 
=  ((ListItem)(ctrlProp.GetValue(control,  null ))).Text;
                    
break ;
                }
                
catch
                {
                    strReturn 
=   "" ;
                }
            }
        }
        
return  strReturn;
    }
}
//Code2
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  System.IO;
using  System.Reflection;
public   partial   class  KB2 : System.Web.UI.Page
{
    
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        ExportGridView();
    }
    
private   void  ExportGridView()
    {
        
string  attachment  =   " attachment; filename=Contacts.xls " ;
        Response.ClearContent();
        Response.AddHeader(
" content-disposition " , attachment);
        Response.ContentType 
=   " application/ms-excel " ;
        StringWriter sw 
=   new  StringWriter();
        HtmlTextWriter htw 
=   new  HtmlTextWriter(sw);
        
//  Create a form to contain the grid
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    
private   void  PrepareGridViewForExport(Control gv)
    {
        Literal l 
=   new  Literal();
        
string  name  =  String.Empty;
        
string  t  =   null ;
        
for  ( int  i  =   0 ; i  <  gv.Controls.Count; i ++ )
        {
            t 
=  gv.Controls[i].GetType().Name;
            
switch  (t)
            {
                
case   " LinkButton " :
                    RenderControlsContent(gv, i, l, (gv.Controls[i] 
as  LinkButton).Text);
                    
break ;
                
case   " DropDownList " :
                    RenderControlsContent(gv, i, l, (gv.Controls[i] 
as  DropDownList).SelectedItem.Text);
                    
break ;
                
case   " CheckBox " :
                    RenderControlsContent(gv, i, l, (gv.Controls[i] 
as  CheckBox).Checked  ?   " True "  :  " False " );
                    
break ;
                
case   " TextBox " :
                    RenderControlsContent(gv, i, l, (gv.Controls[i] 
as  TextBox).Text);
                    
break ;
            }
            
if  (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
    
private   void  RenderControlsContent(Control gv,  int  cindex, Literal l,  string  strValue)
    {
        l.Text 
=  strValue;
        gv.Controls.Remove(gv.Controls[cindex]);
        gv.Controls.AddAt(cindex, l);
    }
}

你可能感兴趣的:(GridView)