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