+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 18

Thread: TextBox to Excel

  1. #1
    Learning Programmer Xystus777 is an unknown quantity at this point Xystus777's Avatar
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Age
    19
    Posts
    59

    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. #2
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  3. #3
    Learning Programmer Xystus777 is an unknown quantity at this point Xystus777's Avatar
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Age
    19
    Posts
    59

    Re: TextBox to Excel

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

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  5. #5
    Learning Programmer Xystus777 is an unknown quantity at this point Xystus777's Avatar
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Age
    19
    Posts
    59

    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.

  6. #6
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  7. #7
    Learning Programmer Xystus777 is an unknown quantity at this point Xystus777's Avatar
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Age
    19
    Posts
    59

    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.

  8. #8
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  9. #9
    Learning Programmer Xystus777 is an unknown quantity at this point Xystus777's Avatar
    Join Date
    Oct 2008
    Location
    Chesterfield, MI
    Age
    19
    Posts
    59

    Re: TextBox to Excel

    string addTNumbers = addTNumberTextBox.Text;

  10. #10
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Excel Automation
    By Xystus777 in forum C# Programming
    Replies: 1
    Last Post: 11-14-2008, 12:48 PM
  2. using VB with excel
    By renoald in forum Visual Basic Programming
    Replies: 26
    Last Post: 10-20-2008, 11:52 AM
  3. Import from excel to Sql in VB.NET?
    By Borin in forum Visual Basic Programming
    Replies: 3
    Last Post: 08-18-2008, 06:21 AM
  4. export to excel problem with IPAddress in URL but working with localhost url
    By mustaque in forum ASP, ASP.NET and Coldfusion
    Replies: 5
    Last Post: 08-04-2008, 07:31 AM
  5. Help on Excel sheets!!!!!!!!!
    By sania21 in forum Visual Basic Programming
    Replies: 2
    Last Post: 07-02-2007, 05:41 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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