To solve this problem, a new method has been added which takes the row number as a parameter, Table.GetNumColumns(row). The old NumColumns property has been removed from the API because it was confusing and could cause runtime exceptions (since there is no way to reliably determine at compile-time whether or not a given table is rectangular).
The following code example demonstrates the correct way to iterate through a table using the new method:
// instantiate WordWriter
WordApplication wa = new WordApplication();
// open a document that contains a table
Document doc = wa.Open("C:\\InputFiles\\DocumentWithTable.doc");
// get the first table in the document
SoftArtisans.OfficeWriter.WordWriter.Table table =
(SoftArtisans.OfficeWriter.WordWriter.Table)doc.get_Elements(Element.Type.Table)[0];
// iterate through the cells of the table and print out the text in each cell
for (int row = 0; row < table.NumRows; row++)
{
for (int col = 0; col < table.GetNumColumns(row); col++)
{
SoftArtisans.OfficeWriter.WordWriterTableCell cell = table[row, col];
Response.Write("cell[" + row + "," + col + "]: " + cell.Text + "<br/>");
}
}
|