This should do it. Note: the inner table has to be handled with the CT classes 
since cells containing anything other than paragraphs does not seem to be 
available yet. Also, every table cell must end with a paragraph. For now, when 
a table cell is created, a blank paragraph is added automatically. I don't 
really like this behavior, but given the requirement that a cell must contain 
at least one paragraph, and the last element in a cell must be a paragraph, I 
am going to have to adjust the code to deal with this in a more user friendly 
manner.

You will, of course, want to add loops, as appropriate to your situation, to 
minimize redundant code :)

Please ask these things on the list in the future so others can benefit from 
your questions.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

public class TableTest {

       /**
       * @param args
       */
       public static void main(String[] args) {
              // Blank Document
              XWPFDocument doc = new XWPFDocument();
              XWPFTable tbl = doc.createTable(2,2);
              XWPFTableCell cell;
              XWPFParagraph p;
              List<XWPFParagraph> pList;
              CTTbl ctTable;
              CTTblPr ctTPr;
              CTTblWidth ctTW;
              CTTblBorders ctBorders;
              CTBorder ctB;
              CTRow ctRow;
              CTTc ctCell;
              CTP ctP;
              CTR ctR;
              CTText ctTx;


              cell = tbl.getRow(0).getCell(0);
              pList = cell.getParagraphs();
              if (pList.size() == 0) {
                     p = cell.addParagraph();
              }
              p = pList.get(0);
              p.createRun().setText("Cell 1, Row 1");

              cell = tbl.getRow(0).getCell(1);
              pList = cell.getParagraphs();
              if (pList.size() == 0) {
                     p = cell.addParagraph();
              }
              p = pList.get(0);
              p.createRun().setText("Cell 2, Row 1");

              cell = tbl.getRow(1).getCell(0);
              pList = cell.getParagraphs();
              if (pList.size() == 0) {
                     p = cell.addParagraph();
              }
              p = pList.get(0);
              p.createRun().setText("Cell 1, Row 2");

              cell = tbl.getRow(1).getCell(1);
              pList = cell.getParagraphs();
              if (pList.size() == 0) {
                     p = cell.addParagraph();
              }
              p = pList.get(0);
              p.createRun().setText("Cell 2, Row 2");
              ctCell = cell.getCTTc();
              ctTable = ctCell.addNewTbl();
              ctTPr = ctTable.addNewTblPr();
              ctTW = ctTPr.addNewTblW();
              ctTW.setW(new BigInteger("0"));
              ctTW.setType(STTblWidth.AUTO);
              ctBorders = ctTPr.addNewTblBorders();
              ctB = ctBorders.addNewTop();
              ctB.setVal(STBorder.SINGLE);
              ctB = ctBorders.addNewLeft();
              ctB.setVal(STBorder.SINGLE);
              ctB = ctBorders.addNewBottom();
              ctB.setVal(STBorder.SINGLE);
              ctB = ctBorders.addNewRight();
              ctB.setVal(STBorder.SINGLE);
              ctB = ctBorders.addNewInsideH();
              ctB.setVal(STBorder.SINGLE);
              ctB = ctBorders.addNewInsideV();
              ctB.setVal(STBorder.SINGLE);
              ctRow = ctTable.addNewTr();
              ctCell = ctRow.addNewTc();
              ctP = ctCell.addNewP();
              ctR = ctP.addNewR();
              ctTx = ctR.addNewT();
              ctTx.setStringValue("Cell 1, Row 1");
              ctCell = ctRow.addNewTc();
              ctP = ctCell.addNewP();
              ctR = ctP.addNewR();
              ctTx = ctR.addNewT();
              ctTx.setStringValue("Cell 2, Row 1");
              ctRow = ctTable.addNewTr();
              ctCell = ctRow.addNewTc();
              ctP = ctCell.addNewP();
              ctR = ctP.addNewR();
              ctTx = ctR.addNewT();
              ctTx.setStringValue("Cell 1, Row 2");
              ctCell = ctRow.addNewTc();
              ctP = ctCell.addNewP();
              ctR = ctP.addNewR();
              ctTx = ctR.addNewT();
              ctTx.setStringValue("Cell 2, Row 2");

              // A paragraph has to be the last element in a table cell.
              p = cell.addParagraph();
              p.createRun().addCarriageReturn();


              // Write the Document in file system
              try {
                     FileOutputStream out = new FileOutputStream(new File(
                                  "tabletest.docx"));
                     doc.write(out);
                     out.close();
                     doc.close();
                     System.out.println("tabletest.docx written successully");
              } catch (FileNotFoundException e) {
                     e.printStackTrace();
              } catch (IOException e) {
                     e.printStackTrace();
              }
       }

}


From: Krishna Vyas Majji [mailto:krishna.ma...@quest-global.com]
Sent: Thursday, September 01, 2016 10:49 AM
To: Murphy, Mark <murphym...@metalexmfg.com>
Subject: RE: How to insert table inside tablerowcell using apache poi

I tried mark I am not getting exactly like this. Do you have any sample code 
snippet?

From: Murphy, Mark [mailto:murphym...@metalexmfg.com]
Sent: Thursday, September 01, 2016 7:03 PM
To: Krishna Vyas Majji
Subject: RE: How to insert table inside tablerowcell using apache poi

This is allowed, and should be possible.

From: Krishna Vyas Majji [mailto:krishna.ma...@quest-global.com]
Sent: Thursday, September 01, 2016 7:45 AM
To: Murphy, Mark <murphym...@metalexmfg.com<mailto:murphym...@metalexmfg.com>>
Subject: How to insert table inside tablerowcell using apache poi

Hi Mark,

Is it possible to create table inside cell using XWPF document
As shown below. Thanks in advance.


Cell 1, Row 1

Cell 2, Row 1

Cell 1, Row 2

Cell 2, Row 2

Cell 1, Row 1

Cell 2, Row 1

Cell 1, Row 2

Cell 2, Row 2





Regards,
Krishna
---Disclaimer------------------------------ This e-mail contains PRIVILEGED AND 
CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s). If 
you are not the intended recipient, please notify the sender by e-mail and 
delete the original message. Opinions, conclusions and other information in 
this transmission that do not relate to the official business of QuEST Global 
and/or its subsidiaries, shall be understood as neither given nor endorsed by 
it. Any statements made herein that are tantamount to contractual obligations, 
promises, claims or commitments shall not be binding on the Company unless 
followed by written confirmation by an authorized signatory of the Company. 
-----------------------------------------------------------------------------------
---Disclaimer------------------------------ This e-mail contains PRIVILEGED AND 
CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s). If 
you are not the intended recipient, please notify the sender by e-mail and 
delete the original message. Opinions, conclusions and other information in 
this transmission that do not relate to the official business of QuEST Global 
and/or its subsidiaries, shall be understood as neither given nor endorsed by 
it. Any statements made herein that are tantamount to contractual obligations, 
promises, claims or commitments shall not be binding on the Company unless 
followed by written confirmation by an authorized signatory of the Company. 
-----------------------------------------------------------------------------------

Reply via email to