Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming AExcel with C#

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
US
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top