Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 18-Jan-12 23:55pm
Edited 18-Jan-12 23:56pm
28.5K
v2
Comments
Jephunneh Malazarte at 19-Jan-12 6:21am
   
can't you access the cell in your excel and assign the value of your textbox there?

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

  Print Answers RSS
0 OriginalGriff 620
1 Maciej Los 610
2 CPallini 255
3 RyanDev 161
4 Peter Leow 125
0 OriginalGriff 5,784
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,009
3 Maciej Los 2,894
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100