We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Syncfusion Feedback


Trusted by the world’s leading companies

Overview

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.


How to create a table in C#

  1. Install NuGet package: Install the Syncfusion.DocIO.Net.Core NuGet package in your project.
  2. Create Word document: Initialize the WordDocument object.
  3. Add section and paragraph: Use the EnsureMinimal method to add a section and a paragraph to the document.
  4. Create table: Create a new table using the AddTable method and specify the number of rows and columns through the ResetCells method.
  5. Add content in table: Utilize the AppendText method to add content in each table cell.
  6. Save the Word document: Save the WordDocument object to the FileStream object.

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);

Table styles

Apply built-in table styles

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);

Apply custom table style

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);




Awards

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.

Scroll up icon