By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Connect to Excel then format within a cell

P: n/a
I need to format text in cell in an Excel worksheet and I need to do this using C#.

I've seen code that will set the format for the whole cell, but I just need to format one character
in that cell.

Example:
The cell has this text in it: H20

I need to use C# to change the "2" to be a subscript.

A similar question was asked before, but I don't think the reply answered the part about only
formatting part of the text in the cell:

( the link will probably wrap )
http://groups.google.com/group/micro...501f0007c0b0f3

TIA for any help on this.
-BEP
Jan 31 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Brian Parker" wrote:
I need to format text in cell in an Excel worksheet and I need to do this using C#.

I've seen code that will set the format for the whole cell, but I just need to format one character
in that cell.

Example:
The cell has this text in it: H20

I need to use C# to change the "2" to be a subscript.

A similar question was asked before, but I don't think the reply answered the part about only
formatting part of the text in the cell:

( the link will probably wrap )
http://groups.google.com/group/micro...501f0007c0b0f3

TIA for any help on this.
-BEP
Brian,

You can use the method called get_Charaters(start, numberOfChars) to get a
range of characters in a cell. Then, apply whatever formatting you wish to
just that set of chars via the Font property.

Here is some sample code that subscripts the second char on cell B4.

Hope this helps,
Jason Vermillion

Excel.Application app = new Excel.ApplicationClass();
app.Visible = true;

app.Workbooks.Open(@"c:\temp\Book1.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet wks = (Excel.Worksheet) app.Sheets["Sheet1"];
wks.get_Range("B4", Type.Missing).set_Value(Type.Missing, "ZZZZZZZ");
wks.get_Range("B4", Type.Missing).get_Characters(2,1).Font.Subscript = true;

Feb 1 '07 #2

P: n/a
On Jan 31, 5:59 pm, Jason Vermillion
<JasonVermill...@discussions.microsoft.comwrote:
You can use the method called get_Charaters(start, numberOfChars) to get a
range of characters in a cell. Then, apply whatever formatting you wish to
just that set of chars via the Font property.
*snip*

Thanks, Jason. Exactly what I needed.

-BEP

Feb 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.