我想在一行中构建一个具有单元格跨度的表格。跨两个单元格的跨度工作正常,但跨度命令不会将两个单元格连接到一个单元格,就像 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();
}
}