How to Apply Table Styles to Word Tables in C# and VB.NET

A table style holds a set of table, row, cell, paragraph, and text formatting that can be applied to tables. Using table styles instead of directly formatting a table saves time in modifying the look of a table and switching to a different table style. It also allows you to reuse a style for several tables to format all your documents consistently.

In this article, we are going to learn how to create, apply, and modify table styles in Word documents programmatically in C# and VB.NET using the Syncfusion .NET Word Library (Essential DocIO) without Microsoft Word or Office interop dependencies.

The Syncfusion Word Library provides comprehensive APIs to apply built-in table styles, modify an existing table style, create new custom table styles, and copy table styles from one Word document to another. It saves you effort and time by helping you automate the formatting of the tables in your Word documents, using table styles programmatically in C# and VB.NET. The resultant documents can be saved as Word documents (DOCX, WordML), PDFs, images, HTML, and more.

Different formatting in Word table styles

After creating a table in a Word document, you can easily format that table by applying a table style. The table styles allow you to define unique formatting for each row and cell of a table through conditional formats. A table style can have 13 types of conditional formats:

  • Whole table—Holds common formatting that can be applied to an entire table.
  • Odd-banded columns—Holds special formatting that can be applied to the odd-banded columns.
  • Even-banded columns—Holds special formatting that can be applied to even-banded columns.
  • Odd-banded rows—Holds special formatting that can be applied to odd-banded rows.
  • Even-banded rows—Holds special formatting that can be applied to even-banded rows.
  • First row—Holds special formatting that can be applied to the first row.
  • Last row—Holds special formatting that can be applied to the last row.
  • First column—Holds special formatting that can be applied to the first column.
  • Last column—Holds special formatting that can be applied to the last column.
  • First row first cell—Holds special formatting that can be applied to the first row’s first cell (top-left corner cell).
  • First row last cell—Holds special formatting that can be applied to the first row’s last cell (top-right corner cell).
  • Last row first cell—Holds special formatting that can be applied to the last row’s first cell (bottom-left corner cell).
  • Last row last cell—Holds special formatting that can be applied to the last row’s last cell (bottom-right corner cell).

Even if a table style has definitions for all of these types of conditional formats, you can enable or disable the special formatting for a table through its table style options. There are six table-style options:

  • Header row
  • First column
  • Last row
  • Last column
  • Banded rows
  • Banded columns

Refer to our documentation for more information about enabling and disabling the style options of a table programmatically.

Apply built-in table styles to a Word table created from scratch in C#

Microsoft Word provides various built-in table styles to customize the look and feel of tables in Word documents. Using Syncfusion Word Library, you can apply any of these built-in table styles to a table programmatically with just a single line of code.

The following code example illustrates how to apply the built-in table style “Medium Shading 1 Accent 1” to a table created from scratch in a Word document (DOCX).

//Creates new Word document instance for Word processing.
using (WordDocument document = new WordDocument())
{
    //Adds a section to the Word document.
    IWSection section = document.AddSection();
    //Sets the page margin
    section.PageSetup.Margins.All = 72;
    //Adds a paragraph to the section.
    IWParagraph paragraph = section.AddParagraph();
    paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; 
    paragraph.ParagraphFormat.AfterSpacing = 20;
    IWTextRange textRange = paragraph.AppendText("Suppliers");
    textRange.CharacterFormat.FontSize = 14;
    textRange.CharacterFormat.Bold = true;
    textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(255, 50, 62, 79);
    //Modifies the font size to 10 for default paragraph style.
    WParagraphStyle style = document.Styles.FindByName("Normal") as WParagraphStyle;
    style.CharacterFormat.FontSize = 10;
    //Adds a table to the section.
    WTable table = section.AddTable() as WTable;
    table.ResetCells(1, 6);
    table[0, 0].Width = 52f;
    table[0, 0].AddParagraph().AppendText("Supplier ID");
    table[0, 1].Width = 128f;
    table[0, 1].AddParagraph().AppendText("Company Name");
    table[0, 2].Width = 70f;
    table[0, 2].AddParagraph().AppendText("Contact Name");
    table[0, 3].Width = 92f;
    table[0, 3].AddParagraph().AppendText("Address");
    table[0, 4].Width = 66.5f;
    table[0, 4].AddParagraph().AppendText("City");
    table[0, 5].Width = 56f;
    table[0, 5].AddParagraph().AppendText("Country");
    //Imports data to the table.
    ImportDataToTable(table);
    //Applies the built-in table style (Medium Shading 1 Accent 1) to the table.
    table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent1);
    //Saves the file in the given path.
    docStream = File.Create(Path.GetFullPath(@"Result.docx"));
    document.Save(docStream, FormatType.Docx);
    docStream.Dispose();
}
Generated Word document with built-in table style Medium Shading 1 Accent 1
Generated Word document with built-in table style Medium Shading 1 Accent 1

The complete working example can be downloaded from this GitHub location.

Modify an existing Word table style

Using Syncfusion Word Library, you can access a table style from a Word document using its style name and modify the properties of the table style programmatically in few lines of code.

The following code example illustrates how to access and modify an existing table style from a Word document (DOCX).

//Creates new Word document instance for Word processing.
using (WordDocument document = new WordDocument())
{
    //Opens the input Word document.
    Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx"));
    document.Open(docStream, FormatType.Docx);
    docStream.Dispose();
    //Gets the table style (Medium Shading 1 Accent 1) from the styles collection.
    WTableStyle tableStyle = document.Styles.FindByName("Medium Shading 1 Accent 1", StyleType.TableStyle) as WTableStyle;
    //Gets the conditional formatting style of the first row (table headers) from the table style.
    ConditionalFormattingStyle firstRowStyle = tableStyle.ConditionalFormattingStyles[ConditionalFormattingType.FirstRow];
    if (firstRowStyle != null)
    {
        //Modifies the background color for first row (table headers).
        firstRowStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.FromArgb(255, 31, 56, 100);
    }
    //Saves the file in the given path.
    docStream = File.Create(Path.GetFullPath(@"Result.docx"));
    document.Save(docStream, FormatType.Docx);
    docStream.Dispose();
}
Table style with first row background color modified
Table style with first row background color modified

The complete working example can be downloaded from this GitHub location.

Create and apply custom table styles to an existing table

Syncfusion Word Library allows you to create a new custom table style and apply it to a table in a Word document. For more details, refer to the documentation.

The following code example illustrates how to create a new custom table style and apply the new custom table style to an existing table in a Word document (DOCX).

//Creates new Word document instance for Word processing.
//Creates new Word document instance for Word processing.
WordDocument document = new WordDocument();
//Opens the input Word document.
Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx"));
document.Open(docStream, FormatType.Docx);
docStream.Dispose();
//Adds a new custom table style.
WTableStyle tableStyle = document.AddTableStyle("CustomStyle") as WTableStyle;
//Applies formatting for whole table.
tableStyle.TableProperties.RowStripe = 1;
tableStyle.TableProperties.ColumnStripe = 1;
tableStyle.TableProperties.Paddings.Top = 0;
tableStyle.TableProperties.Paddings.Bottom = 0;
tableStyle.TableProperties.Paddings.Left = 5.4f;
tableStyle.TableProperties.Paddings.Right = 5.4f;
//Applies conditional formatting for first row.
ConditionalFormattingStyle firstRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstRow);
firstRowStyle.CharacterFormat.Bold = true;
firstRowStyle.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(255, 255, 255, 255);
firstRowStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.Blue;
//Applies conditional formatting for first column.
ConditionalFormattingStyle firstColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstColumn);
firstColumnStyle.CharacterFormat.Bold = true;
//Applies conditional formatting for odd row.
ConditionalFormattingStyle oddRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddRowBanding);
oddRowBandingStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.WhiteSmoke;
//Gets table to apply style.
WTable table = (WTable)document.LastSection.Tables[0];
//Applies the custom table style to the table.
table.ApplyStyle("CustomStyle");
//Saves the file in the given path.
docStream = File.Create(Path.GetFullPath(@"Result.docx"));
document.Save(docStream, FormatType.Docx);
docStream.Dispose();
//Releases the resources of Word document instance.
document.Dispose();
Word document with custom table style
Word document with custom table style

The complete working example can be downloaded from this GitHub location.

Copy table styles from one Word document to another

Syncfusion Word library allows you to access an existing table style from one Word document through its style name and copy the table style to another Word document programmatically in just a few lines of code.

The following code example illustrates how to copy an existing table style – “CustomStyle” from a Word document (DOCX) to another.

//Creates new Word document instance for Word processing.
using (WordDocument document = new WordDocument())
{
    //Opens the input Word document.
    Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx"));
    document.Open(docStream, FormatType.Docx);
    docStream.Dispose();
    //Opens the source Word document containing table style definition.
    WordDocument srcDocument = new WordDocument();
    docStream = File.OpenRead(Path.GetFullPath(@"../../../TableStyles.docx"));
    srcDocument.Open(docStream, FormatType.Docx);
    docStream.Dispose();
    //Gets the table style (CustomStyle) from the styles collection.
    WTableStyle srcTableStyle = srcDocument.Styles.FindByName("CustomStyle", StyleType.TableStyle) as WTableStyle;
    //Creates a cloned copy of table style.
    WTableStyle clonedTableStyle = srcTableStyle.Clone() as WTableStyle;
    //Adds the cloned copy of source table style to the destination document. 
    document.Styles.Add(clonedTableStyle);
    //Releases the resources of source Word document instance.
    srcDocument.Dispose();
    //Gets table to apply style.
    WTable table = (WTable)document.LastSection.Tables[0];
    //Applies the custom table style to the table.
    table.ApplyStyle("CustomStyle");
    //Saves the file in the given path.
    docStream = File.Create(Path.GetFullPath(@"Result.docx"));
    document.Save(docStream, FormatType.Docx);
    docStream.Dispose();
}

The complete working example can be downloaded from this GitHub location.

Summary

Thank you for taking the time to read this article. I hope that it is useful for you and that it has answered your questions on using table styles in a Word document using Syncfusion Word Library.

Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples. Using the library, you can create, edit, merge Word documents and also convert a Word document to a PDF, image, HTML, RTF, and more.

If you are new to our Word Library (Essential DocIO), we highly recommend that you follow our Getting Started guide.

Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a free, 30-day trial here.

If you have any questions about these features, please let us know in the comments below. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are happy to assist you!

Tags:

Share this post:

Related Posts

Leave a comment