Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache poi插入具有不同列号的新表行_Java_Apache_Ms Word_Apache Poi - Fatal编程技术网

Java 使用Apache poi插入具有不同列号的新表行

Java 使用Apache poi插入具有不同列号的新表行,java,apache,ms-word,apache-poi,Java,Apache,Ms Word,Apache Poi,我有一个包含1行2列的Word表,我想使用ApachePOI和Java插入一个包含3列的新行。我想要实现的是: 但我得到的是: 我在互联网上搜索了一个解决方案,但我得到的只是设置列宽,这对我的案例不起作用。有人能帮我解决这个问题吗 这是我从Stack Overflow中的一个示例中提取的代码,用于解决产生第二张图中所示输出的问题: File file = new File("C:\\Users\\USER\\Desktop\\testdoc.docx"); XWPFDocum

我有一个包含1行2列的Word表,我想使用ApachePOI和Java插入一个包含3列的新行。我想要实现的是:

但我得到的是:

我在互联网上搜索了一个解决方案,但我得到的只是设置列宽,这对我的案例不起作用。有人能帮我解决这个问题吗

这是我从Stack Overflow中的一个示例中提取的代码,用于解决产生第二张图中所示输出的问题:

    File file = new File("C:\\Users\\USER\\Desktop\\testdoc.docx");
    XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
    FileOutputStream out = new FileOutputStream(file);

    XWPFTable table = doc.getTableArray(0);
    XWPFTableRow oldRow = table.getRow(0);
    table.insertNewTableRow(1);
    XWPFTableRow newRow = table.getRow(1);

    int indWidth = 0;

    for(int i=0; i < oldRow.getTableCells().size(); i++) {
        int sum = 0;
        BigInteger width = oldRow.getCell(i).getCTTc().getTcPr().getTcW().getW();
        sum += width.intValue();

        indWidth = sum/oldRow.getTableCells().size();
    }

    XWPFTableCell cell;
    for (int i = 0; i < 3; i++) {
        cell = newRow.createCell();

        CTTcPr ctTcPr = cell.getCTTc().addNewTcPr();

        CTTblWidth cellWidth = ctTcPr.addNewTcW();
        cellWidth.setType(oldRow.getCell(0).getCTTc().getTcPr().getTcW().getType());  // sets type of width
        BigInteger width = BigInteger.valueOf(indWidth);
        cellWidth.setW(width);  // sets width

        if (oldRow.getCell(0).getCTTc().getTcPr().getGridSpan() != null) {
            ctTcPr.setGridSpan(oldRow.getCell(0).getCTTc().getTcPr().getGridSpan()); // sets grid span if any
        }

        XWPFRun run = cell.getParagraphs().get(0).createRun();
        run.setText("NewRow C" + i); 
    }

    doc.write(out);
    doc.close();

    System.out.println("Done");

}
File File=new文件(“C:\\Users\\USER\\Desktop\\testdoc.docx”);
XWPFDocument doc=新XWPFDocument(新文件输入流(文件));
FileOutputStream out=新的FileOutputStream(文件);
XWPFTable table=doc.getTableArray(0);
XWPFTableRow oldRow=table.getRow(0);
表.insertNewTableRow(1);
XWPFTableRow newRow=table.getRow(1);
int indWidth=0;
对于(int i=0;i
我可以为java世界提出一个解决方案,但不能用poi

有了pxDoc(,免责声明:我是它的作者之一…),这可以通过以下代码完美实现:

document {
    table {
        row {
            cell { "Column 1" }
            cell { "Column 2" }
        }
        row {
            for (var i = 0; i < 3; i++) {
                cell {
                    "NewRow C" + i
                }
            }
        }
    }
}
文档{
桌子{
划船{
单元格{“第1列”}
单元格{“第2列”}
}
划船{
对于(变量i=0;i<3;i++){
细胞{
“纽罗C”+i
}
}
}
}
}