Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Hi Mahesh
In my code write a query for picking values which you want and export them to excel.
C#
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.
 
Share this answer
 
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..
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900