Remove some columns when export from GridView to Excel


In your export routine, you can create an index list for the columns to be removed and use this list to turn these columns’ visible to false before you export them.

 Here is what I use:

 

 

 protected void btnExportExcel_Click(object sender, EventArgs e)
    {
....

        ArrayList ExcludedColumnsIndexList = new ArrayList();
        ExcludedColumnsIndexList.Add("7");//the 8th column
        ExcludedColumnsIndexList.Add("8");//the 9th column

        ExportToExcel(filename, gridView1, ExcludedColumnsIndexList);

    }

  public static void ExportToExcel(string myFile, GridView gv, ArrayList removeColumnslistIndex)

{

 //Columns' index arraylist (removeColumnslistIndex) of GridView gv
                 
   if  (removeColumnslistIndex!=null)
                    {

                        for (int j = 0; j < removeColumnslistIndex.Count; j++)
                        {
                            if (gv.HeaderRow != null)
                            {
                                gv.HeaderRow.Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false; 
                            }
                            
                            for (int i=0; i< gv.Rows.Count;i++)
                            {
                                gv.Rows[i].Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false; 
                            }
                            
                            if (gv.FooterRow != null)
                            {
                                gv.FooterRow.Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false;
                            }

                        }
                   
   }

  //other code omitted here

 

 

}

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s