xmlvaluedisconnectedexception

q5lcpyga  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(392)

我需要垂直合并单元格,我在stackoverflow找到了一个示例代码,它工作正常,但是之后,当我将xwpfdocument转换为pdf时,我得到了以下错误fr.opensagres.poi.xwpf.converter.core.xwpfconverterexception:org.apache.xmlbeans.impl.values.xmlvaluedisconnectedexception如果执行“cell.removepragraph”,就会发生这种情况。

public void convertToPdf() throws Exception {
    InputStream targetStream = new FileInputStream("table.docx");
    XWPFDocument document = new XWPFDocument(targetStream);

    XWPFTable table = document.getTables().get(0);
    mergeCellVertically(table, 0, 3, 7);
    document.close();

    FileOutputStream out = new FileOutputStream("table.pdf");
    PdfOptions options = PdfOptions.create();
    PdfConverter.getInstance().convert(document, out, options);
}

private void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
    for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
        CTVMerge vmerge = CTVMerge.Factory.newInstance();
        if (rowIndex == fromRow){
            // The first merged cell is set with RESTART merge value
            vmerge.setVal(STMerge.RESTART);
        } else {
            // Cells which join (merge) the first one, are set with CONTINUE
            vmerge.setVal(STMerge.CONTINUE);
            // and the content should be removed
            for (int i = cell.getParagraphs().size(); i > 0; i--) {
                cell.removeParagraph(0);
            }
            cell.addParagraph();
        }
        // Try getting the TcPr. Not simply setting an new one every time.
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setVMerge(vmerge);
        } else {
            // only set an new TcPr if there is not one already
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setVMerge(vmerge);
            cell.getCTTc().setTcPr(tcPr);
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题