Answered How to get TableGrid ?

  • Monday, June 25, 2012 7:34 AM
     
      Has Code

    Hello all,

    Some time ago i created some word documents using Microsoft Interop. Now i have to rewrite the code using open XML SDK.

    I want to have the following :

    TEXT1

    TEXT2

    TEXT3

    TEXT4

    TEXT5

    Here i have 8 cells : TEXT1 = two cells merged ; TEXT2 = three cells merged ; TEXT3 = TEXT4 = TEXT5 = 1 cell.

    My techniques before : Was starting with 8 cells and then i was merging, splitting, depending on the row needs. This was done in order to have cells distributed evenly.

    Now in Open XML. i wrote the following code :

    MainDocumentPart mainPart = package.AddMainDocumentPart(); mainPart.Document = new Document();

    Body body = new Body(); Table myTable = new Table(); TableProperties tableProp = new TableProperties(); TableBorders tableBorders = new TableBorders(); tableBorders.TopBorder = new TopBorder(); tableBorders.BottomBorder = new BottomBorder(); tableBorders.LeftBorder = new LeftBorder(); tableBorders.RightBorder = new RightBorder(); tableBorders.InsideHorizontalBorder = new InsideHorizontalBorder(); tableBorders.InsideVerticalBorder = new InsideVerticalBorder(); tableBorders.TopBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableBorders.BottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableBorders.LeftBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableBorders.RightBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableBorders.InsideHorizontalBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableBorders.InsideVerticalBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tableProp.Append(tableBorders); TableStyle tableStyle = new TableStyle() { Val = "TableGrid" }; TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }; tableProp.Append(tableStyle, tableWidth); myTable.AppendChild(tableProp); TableRow row = new TableRow(); TableRowProperties rowProperties = new TableRowProperties(); TableCellProperties cellProperties = new TableCellProperties(); TableCell tableCell = new TableCell(); rowProperties.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "black" }); row.AppendChild(rowProperties); tableCell = new TableCell(new Paragraph(new Run(new Text("TEXT1")))); cellProperties = new TableCellProperties(new TableCellWidth() { Width = "1250" }); tableCell.AppendChild(cellProperties); row.AppendChild(tableCell); tableCell = new TableCell(new Paragraph(new Run(new Text("TEXT2")))); cellProperties = new TableCellProperties(new TableCellWidth() { Width = "1875" }); tableCell.AppendChild(cellProperties); row.AppendChild(tableCell); tableCell = new TableCell(new Paragraph(new Run(new Text("TEXT3")))); cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625" }); tableCell.AppendChild(cellProperties); row.AppendChild(tableCell); tableCell = new TableCell(new Paragraph(new Run(new Text("TEXT$")))); cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625" }); tableCell.AppendChild(cellProperties); row.AppendChild(tableCell); tableCell = new TableCell(new Paragraph(new Run(new Text("TEXT5")))); cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625" }); tableCell.AppendChild(cellProperties); row.AppendChild(tableCell);

    body.AppendChild(myTable);
    mainPart.Document.AppendChild(body);
    package.MainDocumentPart.Document.Save();

    What i get is the following :

    TEXT1

    TEXT2

    TEXT3

    TEXT4

    TEXT5

    The cells are distributed evenly. How can i manage to have the same distribution of cells in the grid as before ?

    Also once i set the cells width i don;t want them to be auto extende by word. I want to get a word wrap



All Replies

  • Tuesday, June 26, 2012 6:51 AM
    Moderator
     
     Answered

    Hi florea.lucian,

    Thanks for posting in the MSDN Forum.

    Dose your table only have one row which you shown to me? If it is, it think you only need create 5 cells for your table and set the width for every cell to fit your requirest.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, June 26, 2012 2:40 PM
     
      Has Code

    Hello back,

    No, the table is larger. I just displayed here in the post a row to see the concept using the OpenXML.

    What i did is the following : I considered my table having as default 8 cells, every one of them 625 width and when defining a cell that has to be merged, i added a gridSpan property. here is the code snippet:

    TableRow row =new TableRow(); 
    TableCellProperties cellProperties = null;
    TableCell tableCell = null;
    
    TableHeader header = new TableHeader();
    row.AppendChild(header);                
                    
    tableCell = new TableCell(GetParagraph("Text1", false, true));
    cellProperties = new TableCellProperties(new TableCellWidth() { Width = "1250", Type = TableWidthUnitValues.Pct });
    cellProperties.Append(new GridSpan() { Val = 2 });
    cellProperties.Append(new Shading()
       {
           Val = ShadingPatternValues.Clear,
           Color = "auto",
           Fill = "D9D9D9"
       });
    tableCell.AppendChild(cellProperties);
    row.AppendChild(tableCell);
                    
    tableCell = new TableCell(GetParagraph("Text2", false, true));            
    cellProperties = new TableCellProperties(new TableCellWidth() { Width = "1875", Type = TableWidthUnitValues.Pct });
    cellProperties.Append(new GridSpan() { Val = 3 });
    cellProperties.Append(new Shading()
        {
          Val = ShadingPatternValues.Clear,
          Color = "auto",
          Fill = "D9D9D9"
        });
    tableCell.AppendChild(cellProperties);                
    row.AppendChild(tableCell);
                    
    tableCell = new TableCell(GetParagraph("Text3", false, true));
    cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625", Type = TableWidthUnitValues.Pct });
    cellProperties.Append(new Shading()
         {
            Val = ShadingPatternValues.Clear,
            Color = "auto",
            Fill = "D9D9D9"
         });
    tableCell.AppendChild(cellProperties);
    row.AppendChild(tableCell);
                    
    tableCell = new TableCell(GetParagraph("Text4", false, true));
    cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625", Type = TableWidthUnitValues.Pct });
    cellProperties.Append(new Shading()
          {
               Val = ShadingPatternValues.Clear,
               Color = "auto",
               Fill = "D9D9D9"
           });
    tableCell.AppendChild(cellProperties);
    row.AppendChild(tableCell);
                    
    tableCell = new TableCell(GetParagraph("Text5", false, true));
    cellProperties = new TableCellProperties(new TableCellWidth() { Width = "625", Type = TableWidthUnitValues.Pct });
    cellProperties.Append(new Shading()
        {
              Val = ShadingPatternValues.Clear,
              Color = "auto",
              Fill = "D9D9D9"
         });
    tableCell.AppendChild(cellProperties);
    row.AppendChild(tableCell);

    Here the method GeParagraph() returns a Run text that can be bold or false (the two bool parameters).

    This worked for me and for the other rows in the table.

    What i was not able to do  was setting the shading only once on row properties. It did not worked for me. This is why I added in every cell the shading.

    If i did something wrong/hardcoded or it could me done more easily, please feel free to correct me.

    Thank you.

  • Thursday, June 28, 2012 10:52 AM
     
     

    Hi florea.lucian,

    Does there exist some original cells in your table? According to your snippet and screen-shooting I don't think you need 8 cells per row. It just need 5 cells every row.

    Best Regards,

    T.X.


    征诛志异,三让两家王朝
    功同开辟,一桮万古江南

  • Thursday, June 28, 2012 11:52 AM
     
     

    Hello,

    I need the eight cells because in the other rows of the table i used all of them and i want the table to be correctly aligned.

    The code snippet is referring only to the table header (1 row)

  • Saturday, June 30, 2012 3:10 AM
     
     Answered

    Hi florea.lucian,

    I don't think it will work. Word will merge it to 5 cell automately when you open Word Document.

    Have a good day,

    T.X.


    征诛志异,三让两家王朝
    功同开辟,一桮万古江南