Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...

C#
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
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

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.


C#
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);
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900