468,510 Members | 1,735 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

how to export textbox in excel?

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
0 1524

Post your reply

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

Similar topics

reply views Thread by Jason | last post: by
8 posts views Thread by DC Gringo | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
1 post views Thread by JawzX01 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.