Home > Code > C# > How to Build New Table inside MS Word Using Table Styles in C# & VB.NET

How to Build New Table inside MS Word Using Table Styles in C# & VB.NET

by sher azam   on Nov 05, 2013   Category: C#   |  Views: 3774    |  Points: 25   |  Gold 


This technical tip explains how developers can build a table with applying Table Style & expand styles to rows & cells of the table. A table style defines a set of formatting that can be easily applied to a table. Formatting such as borders, shading, alignment and font can be set in a table style and applied to many tables for a consistent appearance. Aspose.Words supports applying a table style to a table and also reading properties of any table style. Table styles are preserved during loading and saving in the following ways:

• Table styles in DOCX and WordML formats are preserved when loading and saving to these formats.
• Table styles are preserved when loading and saving in the DOC format (but not to any other format).
• When exporting to other formats, rendering or printing, table styles are expanded to direct formatting on the table so all formatting is preserved.

In Aspose.Words you can apply a table style by using any of the Table.Style, Table.StyleIdentifier and Table.StyleName properties.

Code shows how to build a new table with a table style applied

[C#]

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
// We must insert at least one row first before setting any table formatting.
builder.InsertCell();
// Set the table style used based of the unique style identifier.
// Note that not all table styles are available when saving as .doc format.
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1;
// Apply which features should be formatted by the style.
table.StyleOptions = TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow;
table.AutoFit(AutoFitBehavior.AutoFitToContents);

// Continue with building the table as normal.
builder.Writeln("Item");
builder.CellFormat.RightPadding = 40;
builder.InsertCell();
builder.Writeln("Quantity (kg)");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Apples");
builder.InsertCell();
builder.Writeln("20");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();

doc.Save(MyDir + "DocumentBuilder.SetTableStyle Out.docx");

[VB.NET]


Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)

Dim table As Table = builder.StartTable()
' We must insert at least one row first before setting any table formatting.
builder.InsertCell()
' Set the table style used based of the unique style identifier.
' Note that not all table styles are available when saving as .doc format.
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1
' Apply which features should be formatted by the style.
table.StyleOptions = TableStyleOptions.FirstColumn Or TableStyleOptions.RowBands Or TableStyleOptions.FirstRow
table.AutoFit(AutoFitBehavior.AutoFitToContents)

' Continue with building the table as normal.
builder.Writeln("Item")
builder.CellFormat.RightPadding = 40
builder.InsertCell()
builder.Writeln("Quantity (kg)")
builder.EndRow()

builder.InsertCell()
builder.Writeln("Apples")
builder.InsertCell()
builder.Writeln("20")
builder.EndRow()

builder.InsertCell()
builder.Writeln("Bananas")
builder.InsertCell()
builder.Writeln("40")
builder.EndRow()

builder.InsertCell()
builder.Writeln("Carrots")
builder.InsertCell()
builder.Writeln("50")
builder.EndRow()

doc.Save(MyDir & "DocumentBuilder.SetTableStyle Out.docx")

Code for how to expand the formatting from styles onto the rows and cells of the table as direct formatting

[C#]

Document doc = new Document(MyDir + "Table.TableStyle.docx");

// Get the first cell of the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
Cell firstCell = table.FirstRow.FirstCell;

// First print the color of the cell shading. This should be empty as the current shading
// is stored in the table style.
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore.ToString());

// Expand table style formatting to direct formatting.
doc.ExpandTableStylesToDirectFormatting();

// Now print the cell shading after expanding table styles. A blue background pattern color
// should have been applied from the table style.
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter.ToString());

[VB.NET]


Dim doc As New Document(MyDir & "Table.TableStyle.docx")

' Get the first cell of the first table in the document.
Dim table As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table)
Dim firstCell As Cell = table.FirstRow.FirstCell

' First print the color of the cell shading. This should be empty as the current shading
' is stored in the table style.
Dim cellShadingBefore As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
Console.WriteLine("Cell shading before style expansion: " & cellShadingBefore.ToString())

' Expand table style formatting to direct formatting.
doc.ExpandTableStylesToDirectFormatting()

' Now print the cell shading after expanding table styles. A blue background pattern color
' should have been applied from the table style.
Dim cellShadingAfter As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
Console.WriteLine("Cell shading after style expansion: " & cellShadingAfter.ToString())

More about Aspose.Words for .NET

Aspose.Words is a word processing component that enables Java & .NET applications to read, write and modify Word documents without using Microsoft Word. Other useful features include document creation, content and formatting manipulation, mail merge abilities, reporting features, TOC updated/rebuilt, Embedded OOXML, Footnotes rendering and support of DOCX, DOC, WordprocessingML, HTML, XHTML, TXT and PDF formats (requires Aspose.Pdf). It supports both 32-bit and 64-bit operating systems. You can even use Aspose.Words to build applications with Mono.

More about Aspose.Words for .NET

- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.aspx

- Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts