Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-2008, 08:52 AM
Xystus777's Avatar   
Learning Programmer
 
Join Date: Oct 2008
Location: Chesterfield, MI
Age: 18
Posts: 59
Rep Power: 0
Xystus777 is an unknown quantity at this point
Send a message via AIM to Xystus777 Send a message via MSN to Xystus777 Send a message via Skype™ to Xystus777
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-20-2008, 11:42 AM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-20-2008, 12:35 PM
Xystus777's Avatar   
Learning Programmer
 
Join Date: Oct 2008
Location: Chesterfield, MI
Age: 18
Posts: 59
Rep Power: 0
Xystus777 is an unknown quantity at this point
Send a message via AIM to Xystus777 Send a message via MSN to Xystus777 Send a message via Skype™ to Xystus777
Default Re: TextBox to Excel

I hate to say this to you...but none of that makes sense to me. I'm a beginner, remember?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-20-2008, 01:02 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-20-2008, 01:04 PM
Xystus777's Avatar   
Learning Programmer
 
Join Date: Oct 2008
Location: Chesterfield, MI
Age: 18
Posts: 59
Rep Power: 0
Xystus777 is an unknown quantity at this point
Send a message via AIM to Xystus777 Send a message via MSN to Xystus777 Send a message via Skype™ to Xystus777
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-20-2008, 01:10 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-20-2008, 01:12 PM
Xystus777's Avatar   
Learning Programmer
 
Join Date: Oct 2008
Location: Chesterfield, MI
Age: 18
Posts: 59
Rep Power: 0
Xystus777 is an unknown quantity at this point
Send a message via AIM to Xystus777 Send a message via MSN to Xystus777 Send a message via Skype™ to Xystus777
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-20-2008, 01:13 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 11-20-2008, 01:20 PM
Xystus777's Avatar   
Learning Programmer
 
Join Date: Oct 2008
Location: Chesterfield, MI
Age: 18
Posts: 59
Rep Power: 0
Xystus777 is an unknown quantity at this point
Send a message via AIM to Xystus777 Send a message via MSN to Xystus777 Send a message via Skype™ to Xystus777
Default Re: TextBox to Excel

string addTNumbers = addTNumberTextBox.Text;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 11-20-2008, 01:52 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -5. The time now is 08:12 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads