none
How to merge Tablecells - Horizontally - Unable to find example RRS feed

  • Question

  • I am trying to merge tableCells (Horizontally) in a table in a word file (Docx) using OpenXML api's. I am trying to use the class HorizontalMerge but unable to get any results.

    Code used:

    TableCellProperties tableCellProperties = new TableCellProperties();
                    HorizontalMerge verticalMerge = new HorizontalMerge()
                    {
                        Val = MergedCellValues.Continue
                    };
                    tableCellProperties.Append(verticalMerge);


                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test1"))));
                    tc.Append(tableCellProperties);
                    tr.Append(tc);


                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test"))));
                    //tc.Append(tableCellProperties);
                    tr.Append(tc);
                    
                    tableFinal.Append(tr);


                    doc.Body.Append(tableFinal);
                    doc.Save();


    Varun Malhotra
    =================
    If my post solves your problem could you mark the post as Answered or Vote As Helpful if my post has been helpful for you.

    Wednesday, February 22, 2012 8:55 PM

Answers

  • Found the solution: You have to create a new HorizontalMerge Object for the new tablecell andassign it to its tableproperties. 
    TableCellProperties tableCellProperties = new TableCellProperties();
                    HorizontalMerge verticalMerge = new HorizontalMerge()
                    {
                        Val = MergedCellValues.Restart
                    };
                    tableCellProperties.Append(verticalMerge);
    
    
                    TableCellProperties tableCellProperties1 = new TableCellProperties();
                    HorizontalMerge verticalMerge1 = new HorizontalMerge()
                    {
                        Val = MergedCellValues.Continue
                    };
                    tableCellProperties1.Append(verticalMerge1);
    
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test1"))));
                    tc.Append(tableCellProperties);
                    tr.Append(tc);
    
                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test"))));
                    tc.Append(tableCellProperties1);
                    tr.Append(tc);
                    
                    tableFinal.Append(tr);
    
                    doc.Body.Append(tableFinal);
                    doc.Save();


    Varun Malhotra
    =================
    If my post solves your problem could you mark the post as Answered or Vote As Helpful if my post has been helpful for you.


    Wednesday, February 22, 2012 9:02 PM

All replies

  • Found the solution: You have to create a new HorizontalMerge Object for the new tablecell andassign it to its tableproperties. 
    TableCellProperties tableCellProperties = new TableCellProperties();
                    HorizontalMerge verticalMerge = new HorizontalMerge()
                    {
                        Val = MergedCellValues.Restart
                    };
                    tableCellProperties.Append(verticalMerge);
    
    
                    TableCellProperties tableCellProperties1 = new TableCellProperties();
                    HorizontalMerge verticalMerge1 = new HorizontalMerge()
                    {
                        Val = MergedCellValues.Continue
                    };
                    tableCellProperties1.Append(verticalMerge1);
    
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test1"))));
                    tc.Append(tableCellProperties);
                    tr.Append(tc);
    
                    tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text("Test"))));
                    tc.Append(tableCellProperties1);
                    tr.Append(tc);
                    
                    tableFinal.Append(tr);
    
                    doc.Body.Append(tableFinal);
                    doc.Save();


    Varun Malhotra
    =================
    If my post solves your problem could you mark the post as Answered or Vote As Helpful if my post has been helpful for you.


    Wednesday, February 22, 2012 9:02 PM
  • thanks for giving sample.
    Tuesday, November 20, 2012 11:58 AM