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 :)

Dani AI

Generated

Short practical answer: obtain the Worksheet object and assign a value to a Range that represents B1. Strings may be written directly, but numeric-looking input (for example "0.50") must be converted to a numeric type before assignment, otherwise Excel will store it as text and show the green-triangle "number stored as text" warning. 's example shows automation-style cell access and correctly points out the VSTO/ActiveSheet entry point; 's follow-up demonstrates the common pitfall of storing numbers as text.

Example (VSTO add-in, C#) — parse input and write a numeric value to B1, then set a numeric format:

using Excel = Microsoft.Office.Interop.Excel;
using System.Globalization;
using System.Runtime.InteropServices;

// inside add-in code
var app = Globals.ThisAddIn.Application;
var sheet = (Excel.Worksheet)app.ActiveWorkbook.ActiveSheet;

double value;
if (double.TryParse(inputTextBox.Text, NumberStyles.Any, CultureInfo.CurrentCulture, out value))
{
    var cell = sheet.Range["B1"];
    cell.NumberFormat = "0.00";    // optional: control display
    cell.Value2 = value;           // assign as numeric, not string
    Marshal.ReleaseComObject(cell);
}
Marshal.ReleaseComObject(sheet);

Troubleshooting notes and best practices: parsing with CultureInfo.CurrentCulture avoids decimal-separator bugs; prepending an apostrophe forces text (so it hides the warning but does not produce a numeric cell); to convert existing text-cells to numbers read the cell text, parse to double and reassign as above; prefer Range["B1"] (fewer implicit COM objects) and call Marshal.ReleaseComObject for ranges/worksheets when automating Excel to avoid orphaned Excel processes; in VSTO avoid releasing the Application object itself.

Recommended Answers

All 4 Replies

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.

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";

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!

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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.