Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello everyone..

Below is my requirement...


How to export selected grid view items to excel sheet and save the excel workbook(sheet) in folder in asp.net


Any Suggestions?
Posted
Comments
_Amy 13-Sep-12 8:19am    
What have you tried yet? Where you stuck?
cunny 13-Sep-12 8:31am    
so far i have tried with below code

protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Doc_id");
dt.Columns.Add("Document_Name");
dt.Columns.Add("Rcname");
dt.Columns.Add("PostedBy");
foreach (GridViewRow row in gvdownloaddocuments.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
if (chk.Checked == true)
{
int i = row.RowIndex;
Label lbl = (Label)gvdownloaddocuments.Rows[i].FindControl("lbl_docid");
Label lbl1 = (Label)gvdownloaddocuments.Rows[i].FindControl("lblFileName");
Label lbl2 = (Label)gvdownloaddocuments.Rows[i].FindControl("lbl_rcname");
Label lbl3 = (Label)gvdownloaddocuments.Rows[i].FindControl("lbl_postedby");
DataRow dr = dt.NewRow();
dr["Doc_id"] = Convert.ToString(lbl.Text);
dr["Document_Name"] = Convert.ToString(lbl1.Text);
dr["Rcname"] = Convert.ToString(lbl2.Text);
dr["PostedBy"] = Convert.ToString(lbl3.Text);

dt.Rows.Add(dr);
}
}
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "selectedrows"));
Response.Charset = "";
System.IO.StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
gvdownloaddocuments.RenderControl(htmlwriter);
// gvdownloaddocuments.Attributes.Add("runat=server");
Response.Write(stringwriter.ToString());
Response.End();


}

public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
--------
But m getting the following error
-----------------
RegisterForEventValidation can only be called during Render();

1 solution

 
Share this answer
 
v3
Comments
cunny 13-Sep-12 8:48am    
Hi Kulkarni..
Thank You So Much for your quick response...
I have followed the below link
http://www.aspdotnet-suresh.com/2011/12/export-selected-gridview-rows-to-excel.html
M done with exporting selected items to Excel. :)
Now my next task is i need to save that excel sheet in folder?
How can i achieve that..?
Prasad_Kulkarni 14-Sep-12 0:15am    
Glad it helps!
Your question is different one now. So, please ask it separately.
cunny 14-Sep-12 0:31am    
Yeah..
I want to save the exported excel sheet in a folder?
Prasad_Kulkarni 14-Sep-12 0:36am    
Ask it seperately Cunny, it is a separate question. Ask it from here[^]
__TR__ 14-Sep-12 2:14am    
My 5!

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