我想在一行中构建一个单元格跨度的表。 跨越两个单元格的跨度工作正常,但跨度命令不会将两个单元格连接到一个,就像Word中所做的那样。
Span Command将第一个Cell的右边框移动到第二个Cell的右边框。
结果是,在带有Span的行中,现在又是其他行中的Cell。
其他单元格为空但可见。
我无法移除不必要的Cell。 对于不必要的Cell的removeCell命令似乎有效,因为在此Cell中使用以下setText命令的Test会导致java.lang.IndexOutOfBoundsException,但在Word文件中,已经可见不必要的Cell。
有任何想法可以解决这个问题吗?
我的设置:Win7-64 dom4j的-1.6.1.jar OOXML-模式-1.1.jar POI-3.12-20150511.jar POI-excelant-3.12-20150511.jar POI-OOXML-3.12-20150511.jar POI-OOXML-架构 - 3.12-20150511.jar POI暂存器,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();
}
}
答案 0 :(得分:1)
显然,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