Click here to Skip to main content
14,325,629 members
Rate this:
Please Sign up or sign in to vote.
See more:
when export to excel the excel file contains the hidden column from gridview.

i explain it deeply.

In GridView I hide the 3rd column and export to excel.

in excel file I need the "C" column with hidden.
Updated 18-Aug-14 20:46pm
Mayank Vashishtha 19-Aug-14 2:55am
How are you binding to the gridview. Is it a datatable?
sankmahesh 25-Aug-14 1:59am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi Mahesh
In my code write a query for picking values which you want and export them to excel.
string str = "your Query";
           ds = da.fill_data(str);
           DataTable dt = new DataTable();
           dt = ds.Tables[0];
           string attachment = "attachment; filename=ABC.xls";
           Response.AddHeader("content-disposition", attachment);
           Response.ContentType = "application/";
           string tab = "";
           foreach (DataColumn dc in dt.Columns)
               Response.Write(tab + dc.ColumnName);
               tab = "\t";
           int i;
           foreach (DataRow dr in dt.Rows)
               tab = "";
               for (i = 0; i < dt.Columns.Count; i++)
                   Response.Write(tab + dr[i].ToString());
                   tab = "\t";

make as answer if it helps You.
sankmahesh 23-Aug-14 1:05am
hi Bhati,

Hi all,

I need to export gridview to excel .I am using the following code for the same



"content-disposition", "attachment; filename=MyExcelFile.xls");

Response.ContentType =


StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);




It gets exported from the grid view to the excel.but the excel needs to be protected sheet with the password generated and hide one column from the code.This is for payroll..

Rate this:
Please Sign up or sign in to vote.

Solution 2

there are articles like that
Exporting DataGrid to Excel, Word and Text Files[^]

or you can use EPPLUS
you can make a generic method to retrieve the datasource
and use it both for grid generation and excel generation
EPPLUS Example :[^]

i think it is quite symple to use and less prone to compatibility error that creating excel from the xml/html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100