Create and inserting Tables into documents is easy with DocX.
Simple Table
- // Create a document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add a Table to this document.
- Table t = document.AddTable(2, 3);
- // Specify some properties for this Table.
- t.Alignment = Alignment.center;
- t.Design = TableDesign.MediumGrid1Accent2;
- // Add content to this Table.
- t.Rows[0].Cells[0].Paragraphs.First().Append("A");
- t.Rows[0].Cells[1].Paragraphs.First().Append("B");
- t.Rows[0].Cells[2].Paragraphs.First().Append("C");
- t.Rows[1].Cells[0].Paragraphs.First().Append("D");
- t.Rows[1].Cells[1].Paragraphs.First().Append("E");
- t.Rows[1].Cells[2].Paragraphs.First().Append("F");
- // Insert the Table into the document.
- document.InsertTable(t);
- document.Save();
- }// Release this document from memory.
The above code will creates a document that looks like the below image.
Paragraphs inside Table Cells can of course contain content such as Images, Hyperlinks and custom text styles as in the below example.
Advance Table
- // Create a document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add a Table to this document.
- Table table = document.AddTable(2, 3);
- // Add a Hyperlink into this document.
- Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
- // Add an Image into this document.
- Novacode.Image i = document.AddImage(@"Logo.png");
- // Create a Picture (Custom View) of this Image.
- Picture p = i.CreatePicture();
- p.Rotation = 10;
- // Specify some properties for this Table.
- table.Alignment = Alignment.center;
- table.Design = TableDesign.LightShadingAccent2;
- // Insert the Table into the document.
- Table t1 = document.InsertTable(table);
- // Add content to this Table.
- t1.Rows[0].Cells[0].Paragraphs.First().AppendHyperlink(h).Append(" is my favourite search engine.");
- t1.Rows[0].Cells[1].Paragraphs.First().Append("This text is bold.").Bold();
- t1.Rows[0].Cells[2].Paragraphs.First().Append("Underlined").UnderlineStyle(UnderlineStyle.singleLine);
- t1.Rows[1].Cells[0].Paragraphs.First().Append("Green").Color(Color.Green);
- t1.Rows[1].Cells[1].Paragraphs.First().Append("Right to Left").Direction = Direction.RightToLeft;
- t1.Rows[1].Cells[2].Paragraphs.First().AppendPicture(p);
- document.Save();
- }// Release this document from memory.
The above code will creates a document that looks like the below image.
Happy coding,
Cathal

how can i set a row header?
ReplyDeleteI have to repeat a row every page.
thanks
Once again great post. You seem to have a good understanding of these themes.When I entering your blog,I felt this . Come on and keep writting your blog will be more attractive. To Your Success!
Delete-----------------
RS Gold Runescape Gold Buy WOW Gold
This comment has been removed by the author.
ReplyDeleteGreat post Corners :-bd
ReplyDeleteI can add row dynamically
This comment has been removed by the author.
ReplyDeleteThanks!
ReplyDeleteIs there a w ay to add cells to the rows dynamically ?
ReplyDelete// Loop through the rows in the Table and insert data from the data source.
ReplyDeletefor (int row = 1; row < invoice_table.RowCount; row++)
{
for (int cell = 0; cell < invoice_table.Rows[row].Cells.Count; cell++)
{
Paragraph cell_paragraph = invoice_table.Rows[row].Cells[cell].Paragraphs[0];
cell_paragraph.InsertText(data.Rows[row - 1].ItemArray[cell].ToString(), false);
}
}
Hi
ReplyDeleteHow can we add points with bullets and numbers?
How can we add points with bullets and Launch x431,launch x431 scanner,launch x431 gds numbers?
ReplyDeleteIs there any way to find a specific table in an existing document and to add rows to it?
ReplyDeleteis there any way to merge columns ?
ReplyDelete