Tables help arrange Word document content in rows and columns. A row is a horizontal collection of cells, a column is a vertical collection of cells, and each cell can contain multiple paragraphs and additional tables. The Syncfusion .NET Word Library (DocIO) allows users to create tables with just a few lines of code.
Here is an example of how to create a table in a Word document in C# using the Syncfusion .NET Word Library.
//Create a new instance of a Word document.
using WordDocument document = new WordDocument();
//Add a section and a paragraph to the document.
document.EnsureMinimal();
//Add a new table to the Word document.
IWTable table = document.Sections[0].AddTable();
//Specify the total number of rows and columns.
table.ResetCells(3, 2);
//Access each table cell and append text.
table[0, 0].AddParagraph().AppendText("Item");
table[0, 1].AddParagraph().AppendText("Price($)");
table[1, 0].AddParagraph().AppendText("Apple");
table[1, 1].AddParagraph().AppendText("50");
table[2, 0].AddParagraph().AppendText("Orange");
table[2, 1].AddParagraph().AppendText("30");
//Save the Word document to FileStream.
using FileStream outputStream = new FileStream("Sample.docx", FileMode.Create, FileAccess.Write);
document.Save(outputStream, FormatType.Docx);
Users can format a table with built-in styles.
//Load an existing Word document as FileStream.
using FileStream inputStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read);
using WordDocument document = new WordDocument(inputStream, FormatType.Docx);
//Access the table.
WTable table = document.Sections[0].Tables[0] as WTable;
//Apply the "LightShading" built-in style to the table.
table.ApplyStyle(BuiltinTableStyle.LightShading);
//Save the Word document to FileStream.
using FileStream outputStream = new FileStream("Sample.docx", FileMode.Create, FileAccess.Write);
document.Save(outputStream, FormatType.Docx);
Users can create a new custom style and format the table with it.
//Load an existing Word document as FileStream.
using FileStream inputStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read);
using WordDocument document = new WordDocument(inputStream, FormatType.Docx);
//Access the table.
WTable table = document.Sections[0].Tables[0] as WTable;
//Create a new custom table style.
WTableStyle tableStyle = document.AddTableStyle("CustomStyle") as WTableStyle;
//Create 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;
//Create conditional formatting for first row.
ConditionalFormattingStyle firstRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstRow);
firstRowStyle.CharacterFormat.Bold = true;
firstRowStyle.CharacterFormat.TextColor = Color.FromArgb(255, 255, 255, 255);
firstRowStyle.CellProperties.BackColor = Color.Blue;
//Create conditional formatting for first column.
ConditionalFormattingStyle firstColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstColumn);
firstColumnStyle.CharacterFormat.Bold = true;
//Create conditional formatting for odd rows.
ConditionalFormattingStyle oddRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddRowBanding);
oddRowBandingStyle.CellProperties.BackColor = Color.WhiteSmoke;
//Apply the custom table style to the table.
table.ApplyStyle("CustomStyle");
//Save the Word document to FileStream.
using FileStream outputStream = new FileStream("Sample.docx", FileMode.Create, FileAccess.Write);
document.Save(outputStream, FormatType.Docx);
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.