We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert 2nd Row in Continuation of First Row

Hi.

Please let me know how to inserting row one by one in a table.

Sunil

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can clone first row and insert cloned row into the table. See the following code:

Row refRow = tab.LastRow;

//Add 5 rows

for (int i = 0; i < 5; i++)

{

//Clone row

Row clone = refRow.Clone(true);

//Remove content from the row

clone.GetChildNodes(NodeType.Paragraph, True).Clear();

foreach (Cell cell in clone.Cells)

{

cell.EnsureMinimum();

}

//insert row

tab.Rows.Add(clone);

}

Hope this helps.

Best regards.

Thanks Alexey.

I am able to add row but that is overwriting lastrow.

Sunil

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please provide more information about your problem. You can also try using the following code:

Row refRow = tab.LastRow;

//Add 5 rows

for (int i = 0; i < 5; i++)

{

//Clone row

Row clone = refRow.Clone(true);

//insert row

tab.Rows.Add(clone);

}

Best regards.

Hi Alexey.

This is not helping. Please see my code here which can help you to help me.

Table tab=builder.startTable();

//First Row

builder.insertCell();

builder.getRowFormat().setHeight(15);

//

builder.write("Product");

cellfor.setWidth(80);

//

builder.insertCell();

cellfor.setWidth(180);

builder.write("Product Description");

//

builder.insertCell();

cellfor.setWidth(50);

builder.write("Qty");

//

builder.insertCell();

cellfor.setWidth(100);

builder.write("Value");

//

builder.endRow();

//Cloning LastRow and then inserting.

com.aspose.words.Row refrow=tab.getLastRow();

refrow.deepClone(true);

com.aspose.words.Row clone=refrow;

//refrow.getChildNodes(NodeType.PARAGRAPH,true).clear();

//for (Cell cell : refrow.getCells()) {

// cell.ensureMinimum();

// }

tab.getRows().add(clone);

//

builder.endTable();

//

doc.save(out,1);

Sunil

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

//Open document

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Table tab=builder.startTable();

//First Row

builder.insertCell();

builder.getRowFormat().setHeight(15);

//

builder.write("Product");

builder.getCellFormat().setWidth(80);

//

builder.insertCell();

builder.getCellFormat().setWidth(180);

builder.write("Product Description");

//

builder.insertCell();

builder.getCellFormat().setWidth(50);

builder.write("Qty");

//

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.write("Value");

//

builder.endRow();

//

builder.endTable();

//Cloning LastRow and then inserting.

com.aspose.words.Row refrow = tab.getLastRow();

com.aspose.words.Row clone = (com.aspose.words.Row)refrow.deepClone(true);;

clone.getChildNodes(NodeType.PARAGRAPH,true).clear();

for (Cell cell : clone.getCells()) {

cell.ensureMinimum();

}

tab.getRows().add(clone);

//save document

doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.

Thanks Alexey.

Row Sucessfully added. Now how to change backgroud color of new added row and text of all cells in newly added row.

Sunil

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think in your case you should build whole table using DocumentBuilder see the following code:

//Open document

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Table tab = builder.startTable();

//First Row

builder.insertCell();

builder.getRowFormat().setHeight(15);

//

builder.write("Product");

builder.getCellFormat().setWidth(80);

//

builder.insertCell();

builder.getCellFormat().setWidth(180);

builder.write("Product Description");

//

builder.insertCell();

builder.getCellFormat().setWidth(50);

builder.write("Qty");

//

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.write("Value");

//

builder.endRow();

//

//Second Row

//Set background

builder.getCellFormat().getShading().setBackgroundPatternColor(java.awt.Color.DARK_GRAY);

builder.insertCell();

builder.getRowFormat().setHeight(15);

//

builder.write("text");

builder.getCellFormat().setWidth(80);

//

builder.insertCell();

builder.getCellFormat().setWidth(180);

builder.write("text");

//

builder.insertCell();

builder.getCellFormat().setWidth(50);

builder.write("text");

//

builder.insertCell();

builder.getCellFormat().setWidth(100);

builder.write("text");

//

builder.endRow();

//

builder.endTable();

//save document

doc.save("C:\\Temp\\out.doc");

Best regards.