How to create Nested tables in MS word using XWPFTable

745 views Asked by At

I need to create a table within an another table cell in MS word using Java. I am using the code:

public class HelloWorldTable { 
    public static void main(String[] args) throws IOException {
        File file = new File("WebContent/MSWordTemplates/WelcomeLetterTable.doc");
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
            String name="Gaurav";
            String letterBody="Congratulations ";
        XWPFDocument document = new XWPFDocument(); 
        XWPFTable tableOne = document.createTable();
        XWPFTable table2 = document.createTable();
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);      
        XWPFTableRow tableOneRow2 = tableOne.createRow(); 
        XWPFTableRow tableOneRow3 = tableOne.createRow(); 
        XWPFTableRow tableOneRow4 = tableOne.createRow(); 
        XWPFTableRow tableOneRow5 = tableOne.createRow();
        XWPFTableRow tableOneRow6 = tableOne.createRow(); 
        XWPFTableRow tableOneRow7 = tableOne.createRow(); 
        XWPFTableRow tableOneRow8 = tableOne.createRow();
        XWPFTableRow tableOneRow9 = tableOne.createRow();
        tableOneRowOne.getCell(0).setText("");
        tableOneRow2.getCell(0).setText("");
        tableOneRow3.getCell(0).setText("");
        tableOneRow4.getCell(0).insertTable(0,table2);
            XWPFTableRow table2row1 = table2.getRow(0); 
            table2row1.getCell(0).setText("Hi");
            table2row1.createCell().setText(name);
        tableOneRow5.getCell(0).setText(letterBody);
        tableOneRow6.getCell(0).setText("");
        tableOneRow7.getCell(0).setText("");
        tableOneRow8.getCell(0).setText("");
        tableOneRow9.getCell(0).setText("");
        OutputStream out = new FileOutputStream(new File("C:/Users/gsaxena/Desktop/W7.doc"));
        document.write(out);
        out.flush();
        out.close();
    }
}

In the Line tableOneRow4.getCell(0).insertTable(0,table2); I tried to use inserTable option for the same but it gave me the following error. I removed this part of the code and rest run fine so this section of the code contains error is sure, but do not have the solution for it.

    Exception in thread "main" java.lang.NoClassDefFoundError: org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTTcImpl$1TblList
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTTcImpl.getTblList(Unknown Source)
    at org.apache.poi.xwpf.usermodel.XWPFTableCell.insertTable(XWPFTableCell.java:396)
    at readAndWrite.HelloWorldTable.main(HelloWorldTable.java:42)
Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTTcImpl$1TblList
    at java.net.URLClassLoader.findClass(URLClassLoader.java:419)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:643)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:345)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:609)
    ... 3 more

Please help.

0

There are 0 answers