Click here to Skip to main content
12,401,189 members (39,207 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML Word Microsoft
Hi All

Below is my table generation code in openxml for Microsoft word in C#.net

I am able to create the table in MS office but I have to fill the background color in my first row of table.

But unable to find out the solution.
Please let me know how do i fill the same color still with bold character in first row of table.


Thanks in Advance...

using (WordprocessingDocument doc
           = WordprocessingDocument.Open(fileName, true))
       {
           // Create an empty table.
           Table table = new Table();
 
           // Create a TableProperties object and specify its border information.
           TableProperties tblProp = new TableProperties(
               new TableBorders(
                   new TopBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   },
                   new BottomBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   },
                   new LeftBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   },
                   new RightBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   },
                   new InsideHorizontalBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   },
                   new InsideVerticalBorder()
                   {
                       Val =
                           new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 1
                   }
               )
           );
          table.AppendChild<TableProperties>(tblProp);
 
    var tr = new TableRow();
 
               var tc1 = new TableCell();
               tc1.Append(new Paragraph(new Run(new Text("S Name"))));
               // Assume you want columns that are automatically sized.

               tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2000" }));
 
               tr.Append(tc1);
Posted 7-Feb-12 1:10am
Comments
masdhasdh 7-Feb-12 7:11am
   
Tried the below code but no luck::

tcp.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "517DBF" });

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

I tried the following and it works well.
I have very slightly modified your code to add a Shading object to the tableCellProperties (tcp)

Hope it helps.

Valery.


var tr = new TableRow();
 
var tc1 = new TableCell();
tc1.Append(new Paragraph(new Run(new Text("S Name"))));
// Assume you want columns that are automatically sized.

var tcp = new TableCellProperties(new TableCellWidth()
              {
                  Type = TableWidthUnitValues.Dxa,
                  Width = "2000",
              });
// Add cell shading.
var shading = new Shading()
{
    Color = "auto",
    Fill = "ABCDEF",
    Val = ShadingPatternValues.Clear
};
 
tcp.Append(shading);
tc1.Append(tcp);
 
tr.Append(tc1);
table.Append(tr);
  Permalink  
v2
Comments
masdhasdh 8-Feb-12 2:56am
   
Thanks a lot Valery...working perfectly...

Regards
Mahendra Varandani

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100