954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

write to a specific cell in excel ?

hi
im trying to create a small excel addin in VS2008
byt i cant figure out how to write lika a string or an int to a specific cell?

for example: i want to write something in cell B1?
how do i do that in the C# code? it must be possible?

i tried searching both google an here but havent found anything :/
please help :)

Krimi
Newbie Poster
9 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Try this:

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"


Just alter the objExcel.Cells(1.1) with the cell you want. Test value can be anything you like.

Michael_Knight
Posting Whiz in Training
271 posts since Jan 2008
Reputation Points: 24
Solved Threads: 13
 

first of all you must install the visual tools for office in your computer
Excel.Worksheet oWorksheet = (Excel.Worksheet)YourApplication.ActiveWorkbook.ActiveSheet;
oWorksheet.Cells[1,1]="populated";

Jugortha
Junior Poster
172 posts since Oct 2007
Reputation Points: 11
Solved Threads: 16
 

I have a question on this.. I'm using Visual Studio 2010, and the above code:

oWorkSheet.Cells[1,1] = "populated";

only works for Strings.. I can't seem to do it for numbers such as:

oWorkSheet.Cells[1,1] = "0.50";

I tried ToString() as well.. and btw the "populated" and "0.50" from user input in a textbox.. any help would be appreciated!

SuperSid
Newbie Poster
2 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I found the problem. The excel's cell had a warning that says "this cell is stored as text or preceded by an apostrophe".

So I solved it by putting an apostrophe "'" before the text.. so the code is:

Excel.Range NewCell = (Excel.Range)xlWorkSheet.Cells[3,3];
NewCell.Value2 = "'" + Actual_Weight.Text;

SuperSid
Newbie Poster
2 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: