How to merge cells (or apply colspan) using XWPFTable in POI in Java?

Weddy picture Weddy · Apr 13, 2013 · Viewed 13.4k times · Source

Creating a table in poi was quite easy but it has very limited tutorials and I cannot find one that can create a simple merged cell in a table in generating a docx file.

Answer

ARK picture ARK · Aug 28, 2013

If you have created table, row inside a table and cell inside a row, you can add gridSpan to cell properties:

if (cell.getCTTc().getTcPr() == null) cell.getCTTc().addNewTcPr();
if (cell.getCTTc().getTcPr().getGridSpan() == null) cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(2);

Note: cell is org.apache.poi.xwpf.usermodel.XWPFTableCell.