Create Tables in Word Documents Using C# and VB.NET
A Table
is a document element used to arrange content in rows and columns. With a table, it's easy to organize and display large amounts of data in a grid-like structure.
GemBox.Document's table concept is the same as in most tables, like the ones you can find in Word or HTML documents with cells, rows, and columns. The difference is that you can create tables programmatically in C# and VB.NET and speed up your Word document handling processes.
- A single
Table
contains manyTableRow
elements in theTable.Rows
collection. - A single
TableRow
contains manyTableCell
elements in theTableRow.Cells
collection. - A single
TableCell
contains many block-level elements in theTableCell.Blocks
collection. - The
Table.Columns
collection doesn't reflect the structure of aTable
, it can be used only to specify a width withTableColumn
.
The following example shows how you can create and populate a simple table.
using GemBox.Document;
using GemBox.Document.Tables;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
int rowCount = %RowCount%;
int columnCount = %ColumnCount%;
var document = new DocumentModel();
var section = new Section(document);
document.Sections.Add(section);
// Create a table with 100% width.
var table = new Table(document);
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
section.Blocks.Add(table);
for (int r = 0; r < rowCount; r++)
{
// Create a row and add it to table.
var row = new TableRow(document);
table.Rows.Add(row);
for (int c = 0; c < columnCount; c++)
{
// Create a cell and add it to row.
var cell = new TableCell(document);
row.Cells.Add(cell);
// Create a paragraph and add it to cell.
var paragraph = new Paragraph(document, $"Cell ({r + 1},{c + 1})");
cell.Blocks.Add(paragraph);
}
}
document.Save("Simple Table.%OutputFileType%");
}
}
Imports GemBox.Document
Imports GemBox.Document.Tables
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim rowCount As Integer = %RowCount%
Dim columnCount As Integer = %ColumnCount%
Dim document As New DocumentModel()
Dim section As New Section(document)
document.Sections.Add(section)
' Create a table with 100% width.
Dim table As New Table(document)
table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
section.Blocks.Add(table)
For r As Integer = 0 To rowCount - 1
' Create a row and add it to table.
Dim row As New TableRow(document)
table.Rows.Add(row)
For c As Integer = 0 To columnCount - 1
' Create a cell and add it to row.
Dim cell As New TableCell(document)
row.Cells.Add(cell)
' Create a paragraph and add it to cell.
Dim paragraph As New Paragraph(document, $"Cell ({r + 1},{c + 1})")
cell.Blocks.Add(paragraph)
Next
Next
document.Save("Simple Table.%OutputFileType%")
End Sub
End Module
You can create a nested table by adding a new table into a cell's TableCell.Blocks
collection.
You can also create a table from an HTML source (e.g. <table>
, <tr>
, <td>
tags) by using the ContentRange.LoadText
or ContentPosition.LoadText
methods, like shown in the Insert HTML and RTF content to Word file example.