1

我想在一行中构建一个具有单元格跨度的表格。跨两个单元格的跨度工作正常,但跨度命令不会将两个单元格连接到一个单元格,就像 Word in 会做的那样。

Span 命令将第一个单元格的右边界移动到第二个单元格的右边界。

结果是,在具有跨度的行中,现在与其他行一样,多了一个单元格。

附加的单元格是空的,但可见。

我无法移除不必要的细胞。对不需要的单元格的 removeCell 命令似乎有效,因为在此单元格中带有以下 setText 命令的测试会导致 java.lang.IndexOutOfBoundsException,但在 Word 文件中,不需要的单元格已经可见。

有没有人有解决这个问题的想法?

我的设置:Win7-64 dom4j-1.6.1.jar ooxml-schemas-1.1.jar poi-3.12-20150511.jar poi-excelant-3.12-20150511.jar poi-ooxml-3.12-20150511.jar poi-ooxml-schemas -3.12-20150511.jar poi-scratchpad-3.12-20150511.jar xmlbeans-2.6.0.jar

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class MainCreateTable {

    public static void setCellSpan(XWPFTableCell cell, int span) {
        if (cell.getCTTc().getTcPr() == null) {
            cell.getCTTc().addNewTcPr();
        }
        if (cell.getCTTc().getTcPr().getGridSpan() == null) {
            cell.getCTTc().getTcPr().addNewGridSpan();
        }
        cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((long) span));
    }

    /**
     *
     * expected Table
     * 
     * ---------------------------------------------|<br>
     * | row 1 cell 1 | row 1 cell 2 | row 1 cell 3 |<br>
     * ---------------------------------------------|<br>
     * | row 2 cell 1                | row 2 cell 2 |<br>
     * ---------------------------------------------|<br>
     * | row 3 cell 1 | row 3 cell 2 | row 3 cell 3 |<br>
     * ---------------------------------------------|<br>
     *
     * generated Table: with an additional empty Cell in Row 2
     * 
     * ---------------------------------------------|<br>
     * | row 1 cell 1 | row 1 cell 2 | row 1 cell 3 |<br>
     * ---------------------------------------------|-|<br>
     * | row 2 cell 1                | row 2 cell 2 | |<br>
     * ---------------------------------------------|-|<br>
     * | row 3 cell 1 | row 3 cell 2 | row 3 cell 3 |<br>
     * ---------------------------------------------|<br>
     *
     */
    public void makeTable() throws IOException {
        String docName = MainCreateTable.class.getSimpleName();
        XWPFDocument document = new XWPFDocument();

        XWPFTable table = document.createTable();
        XWPFTableRow tableRowOne = table.getRow(0);
        tableRowOne.getCell(0).setText("   row 1 cell 1   ");
        tableRowOne.addNewTableCell().setText("   row 1 cell 2   ");
        tableRowOne.addNewTableCell().setText("   row 1 cell 3   ");

        XWPFTableRow tableRowTwo = table.createRow();
        tableRowTwo.getCell(0).setText("   row 2 cell 1   ");
        tableRowTwo.getCell(1).setText("   row 2 cell 2   ");
        setCellSpan(tableRowTwo.getCell(0), 2);
        //tableRowTwo.removeCell(2);
        //tableRowTwo.getCell(2).setText("   row 2 cell 3   ");

        XWPFTableRow tableRowThree = table.createRow();
        tableRowThree.getCell(0).setText("   row 3 cell 1   ");
        tableRowThree.getCell(1).setText("   row 3 cell 2   ");
        tableRowThree.getCell(2).setText("   row 3 cell 3   ");

        FileOutputStream out = new FileOutputStream(new File(docName + ".docx"));
        document.write(out);
        out.close();
    }

    public static void main(String[] args) throws Exception {
        MainCreateTable mainCreateTable = new MainCreateTable();
        mainCreateTable.makeTable();
    }
}
4

1 回答 1

2

显然 XWPFTableRow 仅从包装对象中删除了引用,但将 XML 节点留在了文档中。要删除它们,您必须从 CTRow CTTc 数组中删除元素,如下所示:

CTRow ctRow = row.getCtRow(); //row being a XWPFTableRow
CTTc[] ctTcs = new CTTc[2]; //the column count of the remaining elements 
//Fill the elements leaving out the third element
for(int i = 0; i < ctRow.sizeOfTcArray(); i++) { 
    if(i != 2) {
        ctTcs[i] = ctRow.getTcArray(i);
    }
}
ctRow.setTcArray(ctTcs);

我认为仍然使用 XWPFTableRow.remove(int) 来保持包装器是最新的并不是一个坏主意,但是这个解决方案对我有用。由于您只在最后省略了元素,因此不必进行此测试,因为您可以简单地使用 ctTcs.length

于 2015-08-05T07:56:01.577 回答