如何在poi word的单元格中插入表格?

问题描述 投票:0回答:2

我想在单元格中插入一个表格,我做了一个演示但失败了,有人可以给一些建议吗?

    XWPFDocument document = new XWPFDocument();
    XWPFTable tableTwo = document.createTable(1,1);
    XWPFTableRow tableTwoRow1 = tableTwo.getRow(0);
    tableTwoRow1.getCell(0).setText("aaaaaaaaaa");
    XWPFTable tableOne = document.createTable(2,2);
    XWPFTableRow tableOneRow1 = tableOne.getRow(0);
    XWPFTableRow tableOneRow2 = tableOne.getRow(1);
    tableOneRow1.getCell(0).setText("Test");
    tableOneRow1.getCell(1).setText("Test");
    tableOneRow2.getCell(0).setText("Test");
    tableOneRow2.getCell(1).insertTable(0, tableTwo);
    FileOutputStream fos = new FileOutputStream("D:\\2.docx");
    document.write(fos);
    fos.close();
apache-poi
2个回答
4
投票

使用

XWPFTable
创建的
XWPFDocument.createTable
将始终属于该文档。因此以后不能将其从文档中取出并放入单元格中。

为此,需要XWPFTableCell.insertNewTbl

以下代码可使用实际的

apache poi
版本:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableInTable {

 public static void main(String[] args) throws Exception {

    XWPFDocument document = new XWPFDocument();

    XWPFTable tableOne = document.createTable(2,2);
    XWPFTableRow tablerow = tableOne.getRow(0);
    tablerow.getCell(0).setText("Test");
    tablerow.getCell(1).setText("Test");

    tablerow = tableOne.getRow(1);
    tablerow.getCell(0).setText("Test");

    XWPFParagraph paragraph = tablerow.getCell(1).getParagraphArray(0);
    XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(paragraph.getCTP().newCursor());

    tableTwo.getCTTbl().addNewTblPr().addNewTblBorders().addNewLeft().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewRight().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewTop().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewBottom().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideH().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideV().setVal(
     org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);

    tablerow = tableTwo.createRow();
    tablerow.createCell().setText("aaaaaaaaaa");
    tablerow.createCell().setText("jjjjjjjj"); 
    tablerow = tableTwo.createRow(); 
    tablerow.getCell(0).setText("bbbbbbbbbb"); 
    tablerow.getCell(1).setText("gggggggggg");

    document.write(new FileOutputStream("CreateWordTableInTable.docx"));
    document.close();

 }
}

但我强烈建议不要使用这种方法,而是使用合并单元格。表格单元格中包含的表格很难看。对于

HTML
以及
Word
以及所有其他可以包含表格的文本文档格式都是如此。


0
投票

要将表格添加到单元格中,可以使用 XWPFTableCell 的方法:

public XWPFTable insertNewTbl(XmlCursor cursor) 

所以

XWPFTable table = cell.insertNewTbl(cell.getParagraphArray(cell.getParagraphs().size() - 1));

如果您想将内部表格添加到不是单元格第一段的段落中,则必须向单元格添加一个段落(显然)。在较新版本的 POI(在 5.2.3 上测试)中,您可以只使用 XWPFtableCell 的 addParagraph() 方法,但请记住,此方法在某些早期版本上存在缺陷。例如,5.2.3 上的此方法更新文档中的 IBodyElements 列表:

public void addParagraph(XWPFParagraph p) {
    this.paragraphs.add(p);
    this.bodyElements.add(p);
}

但在之前的版本(4.1.2)中

public void addParagraph(XWPFParagraph p) {
    this.paragraphs.add(p);
}

因此,您必须使用 XMLCursor 来添加段落,因为 insertNewParagraph 方法会更新 IBodyElemets 列表:

void addParagraphToCellUsingCursor(XWPFTableCell cell) {
    XmlCursor cursor = getCursorAfterParagraph(cell.getParagraphArray(cell.getParagraphs().size() - 1));
    XWPFParagraph cellParagraph = cell.insertNewParagraph(cursor);
}

public static XmlCursor getCursorAfterParagraph(XWPFParagraph previous) {
    XmlCursor cursor = previous.getCTP().newCursor();
    cursor.toEndToken();
    cursor.toNextToken();
    return cursor;
}
        
© www.soinside.com 2019 - 2024. All rights reserved.