none
How to add spreadsheet elements with Style to openxml doc RRS feed

  • Question

  • hi

    I am using openXML, using a word .docx file as a template that opens the file, converts to binary[] then modifies then saves and outputs that new document.  That part is ok, but am adding a table acting as spreadsheet elements.

    Question is How to modify the Font style so that in the generated table, its applying the already set "Style1" that comes from the word docx file?  in other words, in the template.docx there is a style called Style1.   how to have the table code implement this style1?  right now its always bold 14pt.  Style has the right font and pitch

                   revenue   expenses   total

    2014    

    2015

     

    Table table1 = new Table();
    
                TableProperties tableProperties1 = new TableProperties();
                TableStyle tableStyle1 = new TableStyle(){ Val = "TableGrid" };
                TableWidth tableWidth1 = new TableWidth(){ Width = "0", Type = TableWidthUnitValues.Auto };
                TableLook tableLook1 = new TableLook(){ Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true };
    
                tableProperties1.Append(tableStyle1);
                tableProperties1.Append(tableWidth1);
                tableProperties1.Append(tableLook1);
    
                TableGrid tableGrid1 = new TableGrid();
                GridColumn gridColumn1 = new GridColumn(){ Width = "2337" };
                GridColumn gridColumn2 = new GridColumn(){ Width = "2337" };
                GridColumn gridColumn3 = new GridColumn(){ Width = "2338" };
                GridColumn gridColumn4 = new GridColumn(){ Width = "2338" };
    
                tableGrid1.Append(gridColumn1);
                tableGrid1.Append(gridColumn2);
                tableGrid1.Append(gridColumn3);
                tableGrid1.Append(gridColumn4);
    
                TableRow tableRow1 = new TableRow(){ RsidTableRowAddition = "00EF1E0F", RsidTableRowProperties = "00EF1E0F" };
    
                TableCell tableCell1 = new TableCell();
    
                TableCellProperties tableCellProperties1 = new TableCellProperties();
                TableCellWidth tableCellWidth1 = new TableCellWidth(){ Width = "2337", Type = TableWidthUnitValues.Dxa };
    
                tableCellProperties1.Append(tableCellWidth1);
    
                Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00EF1E0F", RsidRunAdditionDefault = "006004F2" };
    
                Run run1 = new Run();
                Text text1 = new Text();
                text1.Text = "R1c1";
    
                run1.Append(text1);
    
                paragraph1.Append(run1);
    
                tableCell1.Append(tableCellProperties1);
                tableCell1.Append(paragraph1);
    
                TableCell tableCell2 = new TableCell();
    
                TableCellProperties tableCellProperties2 = new TableCellProperties();
                TableCellWidth tableCellWidth2 = new TableCellWidth(){ Width = "2337", Type = TableWidthUnitValues.Dxa };
    
                tableCellProperties2.Append(tableCellWidth2);
    
                Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00EF1E0F", RsidRunAdditionDefault = "006004F2" };
    
                Run run2 = new Run();
                Text text2 = new Text();
                text2.Text = "R1c2";
    


    Monday, August 3, 2015 3:11 AM

Answers

  • Hi Bradley

    Try this:

    Create a simple document with a bit of text NOT formatted the way you want the table formatted. Press ENTER and format that paragraph mark with the formatting you want for the table.

    Insert a table, type some text in it to make sure it has the correct formatting. Save and close the document.

    Now inspect it in the Productivity Tool to see how Word has coded the formatting - whether it's by-cell or otherwise (more efficient).

    You could also define a Table Style, which would be less code BUT with the problems you've been having and the time constraints you're apparently under added to the "trickiness" of handling table styles I really didn't want to mention it, rather giving you the "safe and simple" approach...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 4, 2015 12:49 PM
    Moderator

All replies

  • Hi Bradley

    Apply the style to the Paragraph or Font (depending on whether this is a paragraph or character style) being appended to the table cells. So for this, from your code:

                Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00EF1E0F", RsidRunAdditionDefault = "006004F2" };
    
                Run run1 = new Run();
                Text text1 = new Text();
                text1.Text = "R1c1";
    
                run1.Append(text1);
    
                paragraph1.Append(run1);
    
                tableCell1.Append(tableCellProperties1);
                tableCell1.Append(paragraph1);

    paragraph1 or run1 needs pPr or rPr (ParagraphProperties / RunProperties) that picks up the style - same as you do in text outside a table.


    Cindy Meister, VSTO/Word MVP, my blog


    Monday, August 3, 2015 4:40 PM
    Moderator
  • Hi Cindy

    ok makes sense, but is there a way to set the style once for the entire table?  I have definitions ready to apply to each cell, seems like so much extra code, so many dozens of lines and inefficient when there should be some way to say table1.style {Val = "Style1"}; 

    i will code it up with each cell having new definitions for the style..

    Monday, August 3, 2015 5:34 PM
  • and also, why is it that you cannot re-use defined fields like RunProperties or RunFonts?  if i define those once, why can't they be applied to subsequent cells?

    just am not understanding the granularity here.  in regular code you can setup a 'Const' value thats set in stone,  Const pi 3.14159  then but here you have to do Const pi2 3.14159  Const pi3 = 3.14159  runproperties1 runproperties2 runproperties3

    Monday, August 3, 2015 5:53 PM
  • Hi Bradley

    Try this:

    Create a simple document with a bit of text NOT formatted the way you want the table formatted. Press ENTER and format that paragraph mark with the formatting you want for the table.

    Insert a table, type some text in it to make sure it has the correct formatting. Save and close the document.

    Now inspect it in the Productivity Tool to see how Word has coded the formatting - whether it's by-cell or otherwise (more efficient).

    You could also define a Table Style, which would be less code BUT with the problems you've been having and the time constraints you're apparently under added to the "trickiness" of handling table styles I really didn't want to mention it, rather giving you the "safe and simple" approach...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 4, 2015 12:49 PM
    Moderator