给表格内单元格内段落添加图片时,如果这个单元格是合并单元格且不是合并单元格的第一行,这个图片会无法被看见,只有手动把这个合并单元格拆分才能重新看见,请问怎么解决
目前我只能获取到这个合并单元格的其中一个单元格,请问我该如何在此基础上获取这个合并单元格的第一行呢
@z2631632737 您可以使用以下代码将合并单元格的内容复制到范围内的第一个单元格中:
Document document = new Document("C:\\Temp\\in.docx");
// Get cells in the document.
Iterable<Cell> cells = document.getChildNodes(NodeType.CELL, true);
for (Cell c : cells)
{
// Check whether cell is horizontally merged.
if (c.getCellFormat().getHorizontalMerge() == CellMerge.FIRST)
{
Cell nextCell = c.getNextCell();
while (nextCell != null && nextCell.getCellFormat().getHorizontalMerge() == CellMerge.PREVIOUS)
{
// Copy all content into the first cell in range.
while (nextCell.hasChildNodes())
c.appendChild(nextCell.getFirstChild());
nextCell = nextCell.getNextCell();
}
}
// Check whether cell is vertically merged.
if (c.getCellFormat().getVerticalMerge() == CellMerge.FIRST)
{
// Get parent row and determine index of the cell in row.
Row r = c.getParentRow();
int cellIndex = r.getCells().indexOf(c);
Row nextRow = r.getNextRow();
while (nextRow != null)
{
Cell nextVerticalCell = nextRow.getCells().get(cellIndex);
if (nextVerticalCell.getCellFormat().getVerticalMerge() == CellMerge.PREVIOUS)
{
// Copy all content into the first cell in range.
while (nextVerticalCell.hasChildNodes())
c.appendChild(nextVerticalCell.getFirstChild());
}
else
{
// Exit loop.
break;
}
nextRow = nextRow.getNextRow();
}
}
}
document.save("C:\\Temp\\out.docx");