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

 protected void btnExportToExcel_Click(object sender, EventArgs e)
      {            

        gvHours.AllowPaging = false;
        gvHours.AllowSorting = false;
        gvHours.DataSource=Cache["dataset"];
        gvHours.DataBind();
        ChangeControlsToValue(gvHours);
        Response.ClearContent();
        string from = dtFrom.SelectedDate.ToShortDateString();
        string to = dtTo.SelectedDate.ToShortDateString();
        Response.AddHeader("content-disposition", "attachment; filename=" + from + "_to_" + to + ".xls");
        Response.ContentType = "application/excel";
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
        HtmlForm hForm = new HtmlForm();
        gvHours.Parent.Controls.Add(hForm);
        hForm.Attributes["runat"] = "server";
        hForm.Controls.Add(gvHours);
        hForm.RenderControl(hTextWriter);
        Response.Write(sWriter.ToString());
        gvHours.AllowPaging = true;
        gvHours.AllowSorting = true;
        gvHours.DataSource = Cache["dataset"];
        gvHours.DataBind();      
        Response.End();      

      }

      private void ChangeControlsToValue(Control gridView)
      {
        Literal literal = new Literal();

        for (int i = 0; i < gridView.Controls.Count; i++)
        {
          if (gridView.Controls[i].GetType() == typeof(LinkButton))
          {
            literal.Text = (gridView.Controls[i] as LinkButton).Text;
            gridView.Controls.Remove(gridView.Controls[i]);
            gridView.Controls.AddAt(i, literal);
          }
          else if (gridView.Controls[i].GetType() == typeof(DropDownList))
          {
            literal.Text = (gridView.Controls[i] as DropDownList).SelectedItem.Text;
            gridView.Controls.Remove(gridView.Controls[i]);
            gridView.Controls.AddAt(i, literal);
          }
          else if (gridView.Controls[i].GetType() == typeof(CheckBox))
          {
            literal.Text = (gridView.Controls[i] as CheckBox).Checked ? "True" : "False";
            gridView.Controls.Remove(gridView.Controls[i]);
            gridView.Controls.AddAt(i, literal);
          }
          if (gridView.Controls[i].HasControls())
          {

            ChangeControlsToValue(gridView.Controls[i]);

          }

        }

      }

      protected void gvHours_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        btnExportToExcel.Visible = true;
      }

      protected void gvHours_SelectedIndexChanged(object sender, EventArgs e)
      {

      }

    }


Please help me!
Posted
Updated 18-Jan-12 23:56pm
v2
Comments
Jephunneh Malazarte 19-Jan-12 6:21am    
can't you access the cell in your excel and assign the value of your textbox there?
[no name] 19-Jan-12 6:24am    
Text box is in out side of grid view. When i click export button i need to save text box value also.

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