Merging Cells in Excel from C#

B

Bjacaruso

Using Excel 2003 I am trying to merge some cells from C# code:

Code:

Excel.ApplicationClass xApp; //Excel App
Excel.Workbook tWB; //Target Workbook
Excel.Worksheet tWS; //Target Worksheet
Excel.Range tR; //Target Range holder
//Start up Excel
xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
//Create the new Workbook and get the sheet
tWB = xApp.Workbooks.Add(Type.Missing);
tWB.Worksheets.Add(Type.Missing, Type.Missing, 1,
Excel.XlSheetType.xlWorksheet);
tWS = (Excel.Worksheet)tWB.Worksheets.get_Item(1);
//Report Title
tR = tWS.get_Range("A1", "M1");
tR.Font.Size = 20;
tR.Font.Bold = true;
tR.ShrinkToFit = false;
tR.MergeCells = true;
tR.Value2 = "Summary Revenue Report";

Not sure why the range is never merged, I try the same thing from VBA (using
VBA cade) and it works, but this C# does everything but merge the cells....
 
C

Cindy M.

Hi =?Utf-8?B?QmphY2FydXNv?=,
Using Excel 2003 I am trying to merge some cells from C# code:
Just replied to the duplicate question in the VSTO forum :)
But, yes, this would be the right place to ask.

To repeat the gist of my reply: you need to use the MERGE
method; MergeCells is just a property that returns the state for
the given range.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
B

Bjacaruso

I actually have tried using the Merge method:

I used this syntax:

tR.Merge(true);

But the result was the same, the cells were not merged...

The docs refrence using an object named Across, not a boolean but I cannot
find any reference to this parameter anywhere.

Again any help with this would be appreciated.
 
C

Cindy M.

Hi Bjacaruso,
I actually have tried using the Merge method:

I used this syntax:

tR.Merge(true);

But the result was the same, the cells were not merged...

The docs refrence using an object named Across, not a boolean but I cannot
find any reference to this parameter anywhere.
Here's what Excel's VBA Help says (since you've used VBA, I don't have to
tell you how to access it):

"Across Optional Variant. True to merge cells in each row of the specified
range as separate merged cells. The default value is False."

I don't understand what it means (although I'd guess it merges down, rather
than across), but I used the default (passed Type.Missing) and it worked just
fine for me...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

frostbb

To merge cells within Excel you first need to define an Excel Range object.
Just about everything one wants to do requires an instantiated Range object.

Excel.Range oMsExcelCellRange;
iColNbr = (int)phtSnpIDColumns[sSelectedParentSnpID];
poMsExcelWorksheet1.Cells[1,iColNbr] =
this.txtSelectedWrParent.Text.ToString();
oMsExcelCellRange =
poMsExcelWorksheet1.get_Range(poMsExcelWorksheet1.Cells[1,iColNbr],poMsExcelWorksheet1.Cells[1,(iColNbr
+ 1)]);
oMsExcelCellRange.Merge(Type.Missing);

Hope this helps.

Barry
in Oregon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top