Click here to Skip to main content
14,325,629 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi
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.
Posted
Updated 18-Aug-14 20:46pm
v2
Comments
Mayank Vashishtha 19-Aug-14 2:55am
   
How are you binding to the gridview. Is it a datatable?
sankmahesh 25-Aug-14 1:59am
   
yes
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.ClearContent();
           Response.AddHeader("content-disposition", attachment);
           Response.ContentType = "application/vnd.ms-excel";
           string tab = "";
           foreach (DataColumn dc in dt.Columns)
           {
               Response.Write(tab + dc.ColumnName);
               tab = "\t";
           }
           Response.Write("\n");
           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";
               }
               Response.Write("\n");
           }
           Response.End();


make as answer if it helps You.
   
v2
Comments
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

Response.ClearContent();

Response.AddHeader(


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

Response.ContentType =


"application/excel";




StringWriter sw = new StringWriter();




HtmlTextWriter htw = new HtmlTextWriter(sw);

Gridview1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();



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..

Thanks..
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 :
http://epplus.codeplex.com/wikipage?title=WebapplicationExample[^]

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