Added row to existing table

Hi,
I am trying to add some additional rows to existing table(In word Document) using aspose word for java.
It adds all new rows in the table’s last cell as sub-table, rather than adding new rows to table.
Please look in the attachment for the resulting document.

Am I missing something in my code?
Here is the source code I am using to add rows:

Document doc = new Document("C:/data/table.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Sections sections = doc.getSections();
Section section = sections.get(0);
Body body = section.getBody();
Tables tableList = body.getTables();
Table t = tableList.get(0);
Cell lCell = t.getLastRow().getLastCell();

int colSize = t.getLastRow().getCells().getCount();
int noOfRows = 10;
builder.moveToCell(0, 1, colSize - 1, 0);
for (int rowIndex = 0; rowIndex
{
    for (int colIndex = 0; colIndex
    {
        builder.insertCell();
        builder.write(rowIndex + ":" + colIndex);
    }
    builder.endRow();
}
builder.endTable();
doc.save("C:/data/out.doc");

Yes, you need to insert “builder.moveTo(t.getNextSibling());” exactly between “builder.moveToCell(…);” and the loop. This will move the cursor to a next paragraph, right after the table.

Note: 1) you still need in “builder.moveToCell(…);” to implicitly preserve formatting of the sample table; 2) The “next paragraph after the table” it’s better to be empty.

Best Regards,

Thanks Konstantin

This solves my problem perfectly.

-Sunil

Always welcom:)

Konstantin:
Note: 1) you still need in “builder.moveToCell(…);” to implicitly preserve formatting of the sample table;

but this results in every table cell identical to the last cell…what if I have cells of different widths/formats?

Edit: solved :

// rows is a List, each row is a String[]

DocumentBuilder builder = new DocumentBuilder(document);
Sections sections = document.getSections();
Section section = sections.get(sectionIndex);
Body body = section.getBody();
Tables tableList = body.getTables();
Table t = tableList.get(tableIndex);
int lastRowIndex = t.getRows().getCount() - 1;

int colSize = t.getLastRow().getCells().getCount();
builder.moveToCell(tableIndex, 1, 0, 0);
builder.moveTo(t.getNextSibling());
for (Iterator rowsIterator = rows.iterator();
rowsIterator.hasNext();)
{
    String[] row = (String[])rowsIterator.next();
    for (int colIndex = 0; colIndex < colSize; colIndex++)
    {
        builder.insertCell();
        builder.write(row[colIndex]);
        builder.moveToCell(tableIndex, lastRowIndex,
        (colIndex + 1) % colSize, 0);
    }
    builder.endRow();
}
builder.endTable();

Hi, Claudio,
Note that Sunill usually asks to solve some complicated cases:)
If you just want to add a new row to an existing table with the same formatting as other table rows, there are more simple options in aspose.words. One of them is just cloning the last row and adding it to the table’s end (like we doing this by hand in MS Word). Here is a code snippet:

@Test
public void TestAddRow() throws Exception
{
    Document doc = new Document("X:\\Aspose\\forum\\data\\table.doc");
    Table table = doc.getSections().get(0).getBody().getTables().get(0);
    Row lastRow = table.getLastRow();
    for (int i = 0; i < 10; i++)
        table.appendChild(lastRow.deepClone(true));
    doc.save("X:\\Aspose\\forum\\data\\out.doc");
}

Best Regards,

thank you very much !

I have also to write in cells (and preserve formatting from the last row).
This is my code:

Table table = document.getSections().get(sectionIndex).getBody().getTables().get(tableIndex);
Row lastRow = table.getLastRow();
for (Iterator rowsIterator = rows.iterator();
rowsIterator.hasNext();)
{
    Row newRow = (Row)lastRow.deepClone(true);
    Cells newCells = newRow.getCells();
    String[] row = (String[])rowsIterator.next();
    int i = 0;
    for (Iterator cellsIterator = newCells.iterator();
    cellsIterator.hasNext();)
    {
        Cell cell = (Cell)cellsIterator.next();
        cell.getFirstParagraph().appendChild(new Run(document, row[i++]));
    }
    table.appendChild(newRow);
}

but it doesn’t preserve the font formatting! How could I do this?

Hi, Claudio,
Paragraph is a block-level node that can contain some number of inline-level nodes. Run is one of the most commonly used inline-level node that contains run of text. Font formatting can be applied to runs and can’t be applied to paragraphs.
You can see this in MS Word too: paragraph formatting includes only alignment, indent etc. Even if you select a whole paragraph and assign to it a new font formatting – really you will assign the formatting to every run inside the paragraph not to paragraph itself (or rather, GUI will do this for you).
So, in your case you just append to the paragraph a new run with the default formatting:)
To preserve some font formatting you have to get the Font property (from where you save it inside the template or create it programmatically) and assign it to a new Run (exactly like as you coping formatting in MS Word). Or another option (again, as in GUI editor): you can insert a text without formatting inside (or instead) of existing (formatted) run. Here is code snippet with using last option. Note that cell’s paragraph have to contain at least one run (at least on symbol) else this method will throw NullPointerException.

@Test
public void TestAddRow() throws Exception
{
    Document doc = new Document("X:\\Aspose\\forum\\data\\table.doc");
    Table table = doc.getSections().get(0).getBody().getTables().get(0);
    Row lastRow = table.getLastRow();
    int firstNewRow = table.getRows().getCount();
    //append to the table end 10 new rows (copies of the last original row)
    for (int i = 0; i < 10; i++)
        table.appendChild(lastRow.deepClone(true));
    //fill in new rows with something
    //Note! it's should be at least one run inside the paragraph else we get NullPointerException.
    Rows rows = table.getRows();
    for (int i = firstNewRow; i < rows.getCount(); i++)
    {
        Cells cells = rows.get(i).getCells();
        for (int j = 0; j < cells.getCount(); j++)
        {
            //for fun new text added to the last run of the last cell's paragraph
            Paragraph paragraph = cells.get(j).getLastParagraph();
            Run lastRun = paragraph.getRuns().get(-1);
            lastRun.setText(lastRun.getText() + "Row #" + i + ",Cell #" + j);
        }
    }
    doc.save("X:\\Aspose\\forum\\data\\out.doc");
}

Regards,

thanks for the crystal clear explanation!
Now it’s working.
If I understand correctly what I read in another thread, this approach (first create the rows, then write text in cells) is faster than my previous cose snippet, right?

I think, both approaches are equal in terms of performance or differ very little. But may be The Profiler thinks differently:) Any way, aspose.words is very fast and you can’t see the difference without a profiler.