Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Programming AExcel with C#

Programming AExcel with C#

Programming AExcel with C#

I'm converting a vb module to C#. The module handles all interfacing requirements to create a fully-formatted Excel document. I have methods to change color, font, etc.

The problem is that for some reason C#'s interpretation of the Excel object is quite a bit different that vb.net.

Here's a vb.net function that works perfectly in vb:

public function centerHeadingText ( xlsObject As Microsoft.Office.Interop.Excel.Application,
strMessage As string,
numRow As string,
strColumn As string,
numRowHeight As double,
numTextSize As double,
strTextColor As String ) As integer
Dim f As integer
dim strRange As String = "A" & numRow & ":" & strColumn & numRow

f = cellAlign ( xlsObject, strRange, xlCenter, xlTop )
f = setRowHeight( xlsObject, numRow, numRowHeight )
f = cellText ( xlsObject, "A" & numRow, strMessage )

xlsObject.Range ( strColumn & numRow ).Select
xlsObject.Selection.Font.Name = "Arial"
xlsObject.Selection.Font.FontStyle = "Bold"
xlsObject.Selection.Font.Size = numTextSize
xlsObject.Selection.Font.ColorIndex = strTextColor

centerHeadingText = 1
end function

What I need to know is how do I do it in C#. First of all, C# doesn't like the 'xlsObject.Range["A"]' construct even though I have found code snippets online that say to do that for C# (any ideas why?). Instead, I have to do: xlsObject.get_Range(strRange, System.Type.Missing).Select();

Then when I try this code: xlsObject.get_Range(strRange, System.Type.Missing).Style.Font.FontName = "Calibri"; the .Font. gets a red squiggly because apparently the Style object doesn't contain a Font object when you select a cell on the worksheet. (Any ideas why not?)

Thanks in advance,

Jerry Scannell

RE: Programming AExcel with C#

If you are going to code for excel 2007 or greater, I would look at this open source project:

I use it all the time and its pretty simple. You may think its easier to convert a vb.net app to c# but in this case, I would implement this new library. With this library, Excel does not need to be installed on the machine your process runs on.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close