1

我需要使用 Java 在 MS word 中的另一个表格单元格中创建一个表格。我正在使用代码:

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();
    }
}

在 LinetableOneRow4.getCell(0).insertTable(0,table2);我尝试使用 inserTable 选项,但它给了我以下错误。我删除了这部分代码,其余运行正常,所以这部分代码包含错误是肯定的,但没有解决方案。

    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

请帮忙。

4

0 回答 0