By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,868 Members | 1,974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,868 IT Pros & Developers. It's quick & easy.

how to export textbox in excel?

P: 21
Hi,

How to export text box values in excel ? I need to export text box values in "excel".(when we click export to excel button)

Code:(when i click Export button i need to save text box value in excel sheet. ) In below code is working properly, But i need to save textbox value. (text box name is "txtBillable")


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  protected void btnExportToExcel_Click(object sender, EventArgs e)
  4.       {            
  5.  
  6.         gvHours.AllowPaging = false;
  7.         gvHours.AllowSorting = false;
  8.         gvHours.DataSource=Cache["dataset"];
  9.         gvHours.DataBind();
  10.         ChangeControlsToValue(gvHours);
  11.         Response.ClearContent();
  12.         string from = dtFrom.SelectedDate.ToShortDateString();
  13.         string to = dtTo.SelectedDate.ToShortDateString();
  14.         Response.AddHeader("content-disposition", "attachment; filename=" + from + "_to_" + to + ".xls");
  15.         Response.ContentType = "application/excel";
  16.         StringWriter sWriter = new StringWriter();
  17.         HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
  18.         HtmlForm hForm = new HtmlForm();
  19.         gvHours.Parent.Controls.Add(hForm);
  20.         hForm.Attributes["runat"] = "server";
  21.         hForm.Controls.Add(gvHours);
  22.         hForm.RenderControl(hTextWriter);
  23.         Response.Write(sWriter.ToString());
  24.         gvHours.AllowPaging = true;
  25.         gvHours.AllowSorting = true;
  26.         gvHours.DataSource = Cache["dataset"];
  27.         gvHours.DataBind();      
  28.         Response.End();      
  29.  
  30.       }
  31.  
  32.       private void ChangeControlsToValue(Control gridView)
  33.       {
  34.         Literal literal = new Literal();
  35.  
  36.         for (int i = 0; i < gridView.Controls.Count; i++)
  37.         {
  38.           if (gridView.Controls[i].GetType() == typeof(LinkButton))
  39.           {
  40.             literal.Text = (gridView.Controls[i] as LinkButton).Text;
  41.             gridView.Controls.Remove(gridView.Controls[i]);
  42.             gridView.Controls.AddAt(i, literal);
  43.           }
  44.           else if (gridView.Controls[i].GetType() == typeof(DropDownList))
  45.           {
  46.             literal.Text = (gridView.Controls[i] as DropDownList).SelectedItem.Text;
  47.             gridView.Controls.Remove(gridView.Controls[i]);
  48.             gridView.Controls.AddAt(i, literal);
  49.           }
  50.           else if (gridView.Controls[i].GetType() == typeof(CheckBox))
  51.           {
  52.             literal.Text = (gridView.Controls[i] as CheckBox).Checked ? "True" : "False";
  53.             gridView.Controls.Remove(gridView.Controls[i]);
  54.             gridView.Controls.AddAt(i, literal);
  55.           }
  56.           if (gridView.Controls[i].HasControls())
  57.           {
  58.  
  59.             ChangeControlsToValue(gridView.Controls[i]);
  60.  
  61.           }
  62.  
  63.         }
  64.  
  65.       }
  66.  
  67.       protected void gvHours_RowDataBound(object sender, GridViewRowEventArgs e)
  68.       {
  69.         btnExportToExcel.Visible = true;
  70.       }
  71.  
  72.       protected void gvHours_SelectedIndexChanged(object sender, EventArgs e)
  73.       {
  74.  
  75.       }
  76.  
  77.     }
  78.  
  79.  
Please help me!(I need to save textbox value when we click export to excel button).
Jan 19 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.