none
How to Produce a Word Table with a Cell which Span multiple ROWS RRS feed

  • Question

  • Hi I'm trying to dynamically write a Table using Open XML and C# to prduce a table cell which spans a number of rows and contains an image. I can't seem to find a property I can understand to do this. I can see that I could use a Grid span if I wanted to span columns, but can't see anything obvious to span rows. Can anyone help?

    Thanks

    Philip

     

    Thursday, April 15, 2010 1:25 PM

All replies

  • Make it with Word, save the .docx file and then open it with the Productivity Tool so as to reflect the code.

     

    Thursday, April 15, 2010 7:14 PM
  • Hi Manonamission,

    Thanks for your question.

    Using the Productivity Tool to "Compare Files" between the file with/without merged cells and "View Part Code", you could see how to generate the sample code. I have also found for a cell which spans multiple rows, it is related to class "VerticalMerge(w:vMerge)", you could look up the detailed class description in the Tool's "Open XML SDK Documentation" section.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Friday, April 16, 2010 2:29 AM
  • Thanks for the feedback. I half understand the merge now, but am still struggling with c# syntax to implement it. If I create a table like this:

    Table table = new Table();
    
            table.Append(new TableProperties());
            table.Append(new TableGrid());
            table.Append(new TableRow
              (new TableCell
                (new Paragraph
                  (new Run
                    (new Drawing((GenerateImageML(imageFileName, imageWidthEMU, imageHeightEMU)))   
                    )))));

    How do I add the Merged property to the Cell? Is this the best way to produce a table at all? Any help gratefully received. Thanks

    Philip

    Tuesday, April 20, 2010 8:26 AM
  • By the Way, the GenerateImageML works and looks like this:

    try
       {
        var drawing =
         new Drawing(
          new wp.Inline(
           new wp.Extent()
           {
            Cx = imageWidthEMU,
            Cy = imageHeightEMU
           },
           new wp.EffectExtent()
           {
            LeftEdge = 19050L,
            TopEdge = 0L,
            RightEdge = 9525L,
            BottomEdge = 0L
           },
           new wp.DocProperties()
           {
            Id = (UInt32Value)1U,
            Name = "Inline Text Wrapping Picture",
            Description = imageFileName
           },
           new wp.NonVisualGraphicFrameDrawingProperties(
            new a.GraphicFrameLocks() { NoChangeAspect = true }),
           new a.Graphic(
            new a.GraphicData(

     

    Is this the syntax I should be using for my Table? Also using the SDK and reflecting code produces a lot of, how can I put it "bloat" and is sometimes not overly hepful.

    Tuesday, April 20, 2010 8:31 AM
  • Hi Philip,

    Thanks for your reply.

    From your code, I could see that there is only one cell in the table. But there should be multiple cells if you need a cell spaning multiple rows. For example, below is the XML content of a 2x2 table in which the cells in the first column are merged:

    <

    w:tbl>

    <w:tblPr>

    <w:tblStyle w:val="TableGrid"/>

    <w:tblW w:w="0" w:type="auto"/>

    <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>

    </w:tblPr>

    <w:tblGrid>

    <w:gridCol w:w="4788"/>

    <w:gridCol w:w="4788"/>

    </w:tblGrid>

    <w:tr w:rsidR="007F126F" w:rsidTr="007F126F">

    <w:tc>

    <w:tcPr>

    <w:tcW w:w="4788" w:type="dxa"/>

    <w:vMerge w:val="restart"/>

    </w:tcPr>

    <w:p w:rsidR="007F126F" w:rsidRDefault="00AD4239">

    <w:r>

    <w:t>Merged cells</w:t>

    </w:r>

    </w:p>

    </w:tc>

    <w:tc>

    <w:tcPr>

    <w:tcW w:w="4788" w:type="dxa"/>

    </w:tcPr>

    <w:p w:rsidR="007F126F" w:rsidRDefault="00AD4239">

    <w:r>

    <w:t>Row 1 Column 2</w:t>

    </w:r>

    </w:p>

    </w:tc>

    </w:tr>

    <w:tr w:rsidR="007F126F" w:rsidTr="007F126F">

    <w:tc>

    <w:tcPr>

    <w:tcW w:w="4788" w:type="dxa"/>

    <w:vMerge/>

    </w:tcPr>

    <w:p w:rsidR="007F126F" w:rsidRDefault="007F126F"/>

    </w:tc>

    <w:tc>

    <w:tcPr>

    <w:tcW w:w="4788" w:type="dxa"/>

    </w:tcPr>

    <w:p w:rsidR="007F126F" w:rsidRDefault="00AD4239">

    <w:r>

    <w:t xml:space="preserve">Row </w:t>

    </w:r>

    <w:bookmarkStart w:id="0" w:name="_GoBack"/>

    <w:bookmarkEnd w:id="0"/>

    <w:r>

    <w:t>2 Column 2</w:t>

    </w:r>

    </w:p>

    </w:tc>

    </w:tr>

    </w:tbl>

    You could see there are 4 cells which is the same as that of a table without any merged cells, and it uses <w:vMerge> tag (in bold) to mark whether it is merged.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Wednesday, April 21, 2010 2:46 AM