Page 1 of 1

How to insert data into a table?

PostPosted: Mon May 02, 2011 6:17 pm
by javabeans
I have a not pad file of customer details..I parsed it into an array list of java objects... Now need to insert those into a table in word document... Have created a woed document with table, but couldn't insert those data into tables..Plzz help.. How we insert data into tables?

Re: How to insert data into a table?

PostPosted: Mon May 02, 2011 10:57 pm
by jason
There are three approaches to consider.

1. Create the table and add the data as a single operation. You can modify TblFactory to do this.

2. Add the data to an existing table (issue: how do you know you have the correct number of rows/columns?). You can use XPath or TraversalUtil to navigate to the table.

3. Use content control databinding, with a repeat on the table row, to inject XML data automatically into the table cells. See www.opendope.org for further info.

For approach 1 or 2, you need to know that a table cell's content list is accessed by tc.getEGBlockLevelElts() (in docx4j 2.6.0; in 2.7.0 you will be able to do tc.getContent() ).

Re: How to insert data into a table?

PostPosted: Mon May 02, 2011 11:25 pm
by javabeans
thank you for the response... I am new to docx4j.. i couldn't find any code to find the correct usage of Xpath..Can you pls post any piece of code here or give any link to such code... thank you

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 1:22 am
by jason
Please see the Getting Started guide, and the sample XPathQuery

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 1:56 am
by javabeans
I have gone through that. What i plan now is to create a table cell and add the content and add it to table row and then to loop it to access more rows... bt i can't create a table cell and add content to that... I have tried it in this way... bt couldn't create a table cell

org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.Tc tc = factory.createTc();
org.docx4j.wml.TcPr tcpr = factory.createTcPr();
tc.setTcPr(tcpr);
CTVerticalJc valign = factory.createCTVerticalJc();
valign.setVal(STVerticalJc.TOP);
tcpr.setVAlign(valign);
org.docx4j.wml.TcPrInner.GridSpan gspan = factory.createTcPrInnerGridSpan();
int gridspan=10;
gspan.setVal(new BigInteger("" + gridspan));
tcpr.setGridSpan(gspan);
tc.getEGBlockLevelElts().add(p);

wordMLPackage.getMainDocumentPart().addObject(p);

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 9:36 am
by jason
You don't say whether you are getting an error, or whether the tc is simply not appearing in your docx. I presume the latter.

You can always check the structure you have created by using XmlUtils.marshaltoString. It would be helpful if you posted the output of same, pretty printed and surrounded by xml forum markup tags.

Glancing at your code, are you adding the tc to a tr?

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 3:50 pm
by javabeans
yaa..the tc is not appearing in the document.. i want to create a tc and add datat to it...then i need to add the tc to a row and loop the same... can u help in this?

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 5:15 pm
by javabeans
I have created a table finally using the code below... But i need help in adding string to a tc... here when i add "hi" all the cells are populated with hi... how can I access individual cells and populate them with different data... pls help...

Tbl tbl = Context.getWmlObjectFactory().createTbl();

String strTblPr = "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">" + "<w:tblStyle w:val=\"TableGrid\"/>"+ "<w:tblW w:w=\"0\" w:type=\"auto\"/>" + "<w:tblLook w:val=\"04A0\"/>" + "</w:tblPr>";
TblPr tblPr = null;

try
{
tblPr = (TblPr)XmlUtils.unmarshalString(strTblPr);
}

catch (JAXBException e)
{
// Shouldn't happen
e.printStackTrace();
}
tbl.setTblPr(tblPr);

TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
tbl.setTblGrid(tblGrid);
int cols = 4;
int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int cellWidthTwips = new Double(Math.floor( (writableWidthTwips/cols ))).intValue();
for (int i=1 ; i<=cols; i++) {
TblGridCol gridCol = Context.getWmlObjectFactory().createTblGridCol();
gridCol.setW(BigInteger.valueOf(cellWidthTwips));
tblGrid.getGridCol().add(gridCol);
}
int rows=5;
for (int j=1 ; j<=rows; j++) {
Tr tr = Context.getWmlObjectFactory().createTr();
tbl.getEGContentRowContent().add(tr);
for (int i=1 ; i<=cols; i++) {

Tc tc = Context.getWmlObjectFactory().createTc();

tr.getEGContentCellContent().add(tc);

TcPr tcPr = Context.getWmlObjectFactory().createTcPr();
tc.setTcPr(tcPr);
TblWidth cellWidth = Context.getWmlObjectFactory().createTblWidth();
tcPr.setTcW(cellWidth);
cellWidth.setType("dxa");
cellWidth.setW(BigInteger.valueOf(cellWidthTwips));
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.Text t = factory.createText();
t.setValue("hi");
org.docx4j.wml.R run = factory.createR();
run.getRunContent().add(t);

p.getParagraphContent().add(run);
tc.getEGBlockLevelElts().add(p);
}
}



wordMLPackage.getMainDocumentPart().addObject(tbl);

Re: How to insert data into a table?

PostPosted: Tue May 03, 2011 9:01 pm
by javabeans
Thank you for the help...I got the result... :)

Re: How to insert data into a table?

PostPosted: Fri Feb 24, 2012 6:48 pm
by rohitErricson
Thanks Jason , JavaBean and J David Eisenberg for the code.
I have modified the final code of Java beans and now its displaying table correctly.

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import org.docx4j.XmlUtils;
import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
import org.docx4j.jaxb.Context;
import org.docx4j.jaxb.NamespacePrefixMapperUtils;
import org.docx4j.model.table.TblFactory;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;
import org.docx4j.wml.Color;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblGrid;
import org.docx4j.wml.TblGridCol;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.Tc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.Tr;


public class Test {

static WordprocessingMLPackage wordMLPackage = null;
static String inputfilepath;
static boolean save;

public static void main(String[] args) throws Exception {
String inputfilepath;

try {
// getInputFilePath(args);
inputfilepath = System.getProperty("user.dir")
+ "/CreateWordprocessingMLDocument_out.docx";
} catch (IllegalArgumentException e) {

inputfilepath = System.getProperty("user.dir")
+ "/CreateWordprocessingMLDocument_out.docx";
}

save = (inputfilepath == null ? false : true);

System.out.println("Creating package..");
wordMLPackage = WordprocessingMLPackage.createPackage();

wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title",
"Hello world");

wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!");
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();

// Let's add a table
int writableWidthTwips = wordMLPackage.getDocumentModel().getSections()
.get(0).getPageDimensions().getWritableWidthTwips();
int cols = 5;
int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols)))
.intValue();

// Tbl tbl = TblFactory.createTable(3, 3, cellWidthTwips);

Tbl tbl = Context.getWmlObjectFactory().createTbl();

// ///////////////////
String strTblPr = "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION
+ ">" + "<w:tblStyle w:val=\"TableGrid\"/>"
+ "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
+ "<w:tblLook w:val=\"04A0\"/>" + "</w:tblPr>";
TblPr tblPr = null;

try {
tblPr = (TblPr) XmlUtils.unmarshalString(strTblPr);
}

catch (JAXBException e) {
// Shouldn't happen
e.printStackTrace();
}
tbl.setTblPr(tblPr);

TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
tbl.setTblGrid(tblGrid);

int writableWidthTwips1 = wordMLPackage.getDocumentModel()
.getSections().get(0).getPageDimensions()
.getWritableWidthTwips();
int cellWidthTwips1 = new Double(
Math.floor((writableWidthTwips / cols))).intValue();

for (int i = 0; i < cols; i++) {
TblGridCol gridCol = Context.getWmlObjectFactory()
.createTblGridCol();
gridCol.setW(BigInteger.valueOf(cellWidthTwips));
tblGrid.getGridCol().add(gridCol);
}

List<String> ls = new ArrayList<String>();
ls.add("rohit");
ls.add("Dwivedi");
ls.add("hi");
ls.add("hello");
ls.add("Byee");

Tc tc = null;
Tr tr = null;
int rows = 3;
for (int j = 0; j < rows; j++) {
tr = Context.getWmlObjectFactory().createTr();
// tbl.getEGContentRowContent().add(tr);
// for (int i = 1; i <= cols; i++) {
for (int i = 0; i < cols; i++) {
tc = Context.getWmlObjectFactory().createTc();

TcPr tcPr = Context.getWmlObjectFactory().createTcPr();
tc.setTcPr(tcPr);
TblWidth cellWidth = Context.getWmlObjectFactory()
.createTblWidth();
tcPr.setTcW(cellWidth);
cellWidth.setType("dxa");
cellWidth.setW(BigInteger.valueOf(cellWidthTwips));
org.docx4j.wml.ObjectFactory factory1 = Context
.getWmlObjectFactory();
org.docx4j.wml.P p1 = factory.createP();
org.docx4j.wml.Text t1 = factory.createText();
// ls.add("val :" + i);
t1.setValue(ls.get(i));
org.docx4j.wml.R run1 = factory.createR();
run1.getRunContent().add(t1);

p1.getParagraphContent().add(run1);
tc.getEGBlockLevelElts().add(p1);
tr.getEGContentCellContent().add(tc);

}

tbl.getEGContentRowContent().add(tr);
}

wordMLPackage.getMainDocumentPart().addObject(tbl);

if (save) {
wordMLPackage.save(new java.io.File(inputfilepath));
System.out.println("Saved " + inputfilepath);
}

System.out.println("Done.");

}

}