【问题标题】:Table inside a Tablecell - nested Tables with Apache POITablecell 内的表格 - 使用 Apache POI 的嵌套表格
【发布时间】:2015-08-21 11:26:41
【问题描述】:

您好,我正在使用 apache POI,我必须使用它创建 docx 文件。 现在我遍历一个 html 文档并获取标签以创建一个有效的 docx 文档。

当我在表格单元格中有一个表格时,无法正确显示。

<table>
    <tr>
        <td style="text-align: left">Status</td>
        <td>
            <table>
                <tr>
                    <td>test</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

从 docx 加载带有嵌套表的正确文件是没有问题的。

     FileOutputStream fos = null;
            XWPFDocument document = null;
            try {
                fos = new FileOutputStream(docRes);
                if (fileType == docType.DOCX) {
                    try {
                        String fileName = "D:\\test.docx";

                        if (!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) {
                            throw new FileFormatException();
                        } else {
                            XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName));
                            List<XWPFTable> table = doc.getTables();

                        }
                    }
                }
            }

当我再次保存此文件时,我会在 Word 中获得一个带有 POI 的嵌套表格。

但是如何在不加载正确的 docx 文件的情况下创建嵌套表呢?

我检查了许多选项,但已经简单的解决方案不起作用。

            document = new XWPFDocument();
            XWPFTable tableOne = document.createTable();
            XWPFTableRow tableOneRow1 = tableOne.getRow(0);
            XWPFTableRow tableOneRow2 = tableOne.createRow();
            tableOneRow1.getCell(0).setText("Test");
            tableOneRow1.addNewTableCell();
            tableOneRow1.getCell(1).setText("Test");
            tableOneRow2.getCell(0).setText("Test");
            tableOneRow2.addNewTableCell();
            tableOneRow2.getCell(1).setText("include nestedTable");

            XWPFTable tableTwo = document.createTable();
            XWPFTableRow tableTwoRow1 = tableTwo.getRow(0);
            tableTwoRow1.getCell(0).setText("Test");
            tableTwoRow1.addNewTableCell();
            tableTwoRow1.getCell(0).setText("nestedTable");

            tableOneRow2.getCell(1).insertTable(0, tableTwo);

与从 docx 直接加载的变体不同的是,在自制解决方案中,文档在文档根目录中有两个表。

如何构建嵌套表?

谢谢

祝菲利克斯

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    按照这个link,它可能是这样的:

        public static void main(String[] args) throws Exception {
        FileOutputStream out = new FileOutputStream(new File("word.docx"));
        XWPFDocument document = new XWPFDocument();
        XWPFTable tableOne = document.createTable();
        XWPFTableRow tableOneRow1 = tableOne.getRow(0);
        XWPFTableRow tableOneRow2 = tableOne.createRow();
        tableOneRow1.getCell(0).setText("Test11");
        tableOneRow1.addNewTableCell();
        tableOneRow1.getCell(1).setText("Test12");
        tableOneRow2.getCell(0).setText("Test21");
        tableOneRow2.addNewTableCell();
    
        XWPFTableCell cell = tableOneRow2.getCell(1);
        CTTbl ctTbl = cell.getCTTc().addNewTbl();
        ctTbl = cell.getCTTc().addNewTbl();
        CTTblPr tblPr = ctTbl.addNewTblPr();
        cell.removeParagraph(0);
        cell.getCTTc().addNewP();
    
        XWPFTable tableTwo = new XWPFTable(ctTbl,cell);
        XWPFTableRow tableTwoRow1 = tableTwo.getRow(0);
        tableTwoRow1.getCell(0).setText("nestedTable11");
        tableTwoRow1.addNewTableCell();
        tableTwoRow1.getCell(1).setText("nestedTable12");
    
        document.write(out);
        out.close();
    }
    

    或者使用 insertNewTbl(XmlCursor cursor) 方法:

            XWPFParagraph paragraph = tablerow.getCell(1).getParagraphs().get(0);
        XmlCursor cursor = paragraph.getCTP().newCursor();
        XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(cursor);
    

    【讨论】:

      【解决方案2】:

      我还没有对此进行测试,但您可能想查看 XWPFTableCell 类的文档
      public void insertTable(int pos,XWPFTable table) 方法似乎正是您正在寻找的。​​p>

      【讨论】:

      • 谢谢,但是你怎么能看到我在上一条指令中使用了这种方法。是的,你是对的,这种方法看起来不错,但她不适合我。
      • 抱歉,我好像错过了那句话。我实际上自己测试了这个功能,但它似乎什么也没做(或者我做错了)。去年我也需要创建嵌套表并且无法找到一个非常可悲的解决方案。我曾经从 poi 邮件列表(或类似的东西)中读过一个线程,但我无法找到它知道的。提到了一个解决方案,但我需要使用的旧 poi 版本不支持该功能。也许如果你有时间,你可以找到那个线程。
      猜你喜欢
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2010-11-23
      • 2017-12-26
      • 2014-07-02
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多