Hi, I need to create a table with 10 columns and 5 rows with Words. All of the code examples for table creation that I have seen have been to call InsertRow and then InsertCell once for each column in the row. Is there a function that will create a table given the number of columns and rows? Thanks very much!
Hi William,
Thanks for your inquiry. There is on such APIs to create a table with given number of columns and rows. However, you can achieve your requirements using following code example. Hope this helps you.
Document doc = new Document();
Table outerTable = CreateTable(doc, 5, 10, "Cell's text");
// Add it to the document body.
doc.FirstSection.Body.AppendChild(outerTable);
doc.Save(MyDir + "Out v16.7.0.docx");
///
/// Creates a new table in the document with the given dimensions and text in each cell.
///
private static Table CreateTable(Document doc, int rowCount, int cellCount, string cellText)
{
Table table = new Table(doc);
// Create the specified number of rows.
for (int rowId = 1; rowId <= rowCount; rowId++)
{
Row row = new Row(doc);
table.AppendChild(row);
// Create the specified number of cells for each row.
for (int cellId = 1; cellId <= cellCount; cellId++)
{
Cell cell = new Cell(doc);
row.AppendChild(cell);
// Add a blank paragraph to the cell.
cell.AppendChild(new Paragraph(doc));
// Add the text.
cell.FirstParagraph.AppendChild(new Run(doc, cellText));
}
}
return table;
}
Thank you for your reply, Tahir. Because I am moving the cursor to a bookmark with builder.MoveToBookmark (where builder is a DocumentBuilder) I don’t see how I would append the table to doc. However, I used InsertCell and EndRow to build my table.
Hi William,
Thanks for your inquiry. After moving the cursor to your desired location, you can get the current paragraph using DocumentBuilder.CurrentParagraph property. You can use CompositeNode.InsertAfter/InsertBefore method to insert the specified node immediately after/before the specified reference node.
If you still face problem, please share your input and expected output documents here for our reference. We will then provide you more information about your query along with code.
Thanks again, Tahir. Although I haven’t tried your code, what makes me reluctant to use it is that the contents of all the cells will be the same. So I have done it some other way. I could use your code if, after creating the table, there is a function that I could use to move the cursor to any cell given row and column and write text to the cell. As far as I can tell the only way to put text in a table cell is to write text to it just after it is inserted. I think this means that a general function for creating a table cannot be written because writing the data has to be done during creation of the table. If there is something wrong with my thinking please let me know! Many thanks for your help.
Hi William,
Thanks for sharing the detail. You can insert the text in the specific table’s cell after creating the table. Please use DocumentBuilder.MoveToCell method to move the cursor to a table cell in the current section and write the text using DocumentBuilder.Write method. Please refer to the following article. Hop this helps you.
Document doc = new Document();
Table outerTable = CreateTable(doc, 5, 10);
// Add it to the document body.
doc.FirstSection.Body.AppendChild(outerTable);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToCell(0, 2, 4, 0);
builder.Write("Aspose.Words");
doc.Save(MyDir + "Out v16.7.0.docx");
private static Table CreateTable(Document doc, int rowCount, int cellCount)
{
Table table = new Table(doc);
// Create the specified number of rows.
for (int rowId = 1; rowId <= rowCount; rowId++)
{
Row row = new Row(doc);
table.AppendChild(row);
// Create the specified number of cells for each row.
for (int cellId = 1; cellId <= cellCount; cellId++)
{
Cell cell = new Cell(doc);
row.AppendChild(cell);
}
}
return table;
}
Thanks very much, Tahir. Great stuff. Exactly what I need. Thanks for your patience.
Hi William,
Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.