Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: TextBox to Excel

  1. #1
    Xystus777's Avatar
    Xystus777 is offline Learning Programmer
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Posts
    59
    Rep Power
    0

    Question TextBox to Excel

    Hello everyone,

    I know how to take a DataGridView and put it into Excel, but how can I take the text that's in a textbox and put it into a specific cell in Excel?

    Here's my code for DataGridViewToXLS:

    Code:
    //this whole section will transfer everything to Excel, from here to the end. 
                Excel.Application excelApp = new Excel.Application(); 
                Excel._Workbook excelWB; 
                Excel._Worksheet excelWS; 
                Excel.Range excelRng; 
                 
                 
                //Opens a new instance 
                excelWB = (Excel._Workbook)(excelApp.Workbooks.Add(Missing.Value)); 
                excelWS = (Excel._Worksheet)excelWB.ActiveSheet; 
     
                int colIndex = 0; 
                foreach (DataGridViewColumn column in dataGridView1.Columns) 
                { 
                    if (column.Visible) 
                    { 
                        //Export 
                        excelWS.Cells[1, colIndex + 1] = column.HeaderText; 
                        for (int row = 1; row < dataGridView1.Rows.Count; row++) 
                        { 
                            excelWS.Cells[row + 1, colIndex + 1] = dataGridView1[colIndex, row - 1].Value; 
                        } 
                        colIndex++; 
                         
                    } 
                }
    However, I would like to have TextBoxTextToXLS. Thanks.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: TextBox to Excel

    Automate Excel. Import the library, then instantiate Excel.Application to an object variable.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Xystus777's Avatar
    Xystus777 is offline Learning Programmer
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Posts
    59
    Rep Power
    0

    Re: TextBox to Excel

    I hate to say this to you...but none of that makes sense to me. I'm a beginner, remember?

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: TextBox to Excel

    OK.

    Add a reference to Microsoft Office. Then import the namespace. Then, create a new variable and call it "app" like so:

    Code:
    Excel.Application app = new Excel.Application();
    You can now manipulate the app object.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    Xystus777's Avatar
    Xystus777 is offline Learning Programmer
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Posts
    59
    Rep Power
    0

    Re: TextBox to Excel

    Code:
    Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWB;
            Excel.Worksheet xlWS;
            Excel.Range xlRng;
    
            string workbookPath = "C:\\Documents and Settings\\adavis\\My Documents\\Area51\\T-Numbers Data";
    
    private void addTNumberBtn_Click(object sender, EventArgs e)
            {
                xlApp.Workbooks.Open(workbookPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    
                int colIndex = 0;
                string addTNumbers = addTNumberTextBox.Text;
                xlWS.Cells[1, 1] = addTNumbers;
                
    
                //Save the file to where you specified
                xlWB.Save();
                xlApp.Visible = true;
            }
    That's what I have right now. I know how to create an excel app, but I'm having trouble with the textbox it appears. It was kind of working, but now how I want it. Now i'm getting an error about instances.

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: TextBox to Excel

    What is the error message?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Xystus777's Avatar
    Xystus777 is offline Learning Programmer
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Posts
    59
    Rep Power
    0

    Re: TextBox to Excel

    It builds and compiles just fine, but when I run it, and click my button, it says:

    Object reference not set to an instance of an object.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: TextBox to Excel

    Which line of the code does it stop on?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Xystus777's Avatar
    Xystus777 is offline Learning Programmer
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Posts
    59
    Rep Power
    0

    Re: TextBox to Excel

    string addTNumbers = addTNumberTextBox.Text;

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: TextBox to Excel

    Are you absolutely sure the text box is called addTNumberTextBox?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can't take value from textbox
    By socheata in forum PHP Development
    Replies: 6
    Last Post: 09-24-2010, 03:17 AM
  2. Excel 2007 and excel 2010
    By secondrate in forum Visual Basic Programming
    Replies: 1
    Last Post: 06-16-2010, 04:49 AM
  3. Import form Excel, filtr, export to excel
    By seatcordobawrc in forum Pascal and Delphi
    Replies: 1
    Last Post: 03-30-2010, 02:30 PM
  4. Textbox Value
    By Bioshox in forum PHP Development
    Replies: 6
    Last Post: 02-27-2010, 02:11 PM
  5. help me about textbox in vb net 2003
    By saint_xaver in forum Visual Basic Programming
    Replies: 1
    Last Post: 08-31-2008, 10:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts