473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Connect to Excel then format within a cell

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
2 2878
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: Otie | last post by:
I am trying to copy the cell contents in an MSFLXGRD control (using VB5) into Excel, retaining the foreground colors of the text and numbers. I have tried using the Clipboard.SetText...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
4
by: Rich Wallace | last post by:
Hi all, I have a VB app that runs and manages individual XLS files within a single COM object. Upon processing the final fie, I attempt to close out the EXCEL object and release it using...
6
by: Kevin Humphreys | last post by:
Hi There, I am trying to export a recordset to an excel file using the Content Type below in the header. Response.ContentType = "application/vnd.ms-excel" Which works fine however the...
6
by: McKirahan | last post by:
I an using ASP to read a database table and generate an HTML table which is save via FSO with a file extension of .xls which opens up in MS-Excel. I am inserting several lines of text into a cell...
4
by: wellscrambled | last post by:
Folks, Probably don't have the right forum here, but this is all a bit new to me. I have a web site that sends me a daily email with an excel spreadsheet attachment that contains some customer...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
2
by: ravir81 | last post by:
Hi, I am working on excel reporting using Perl. I am facing problem with writing the header part only once for all the excels created using Perl. Here is the code : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.