Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI merge cells from a table in a Word document

I need to have a table with the cells on the first and second row merged.

Something like this:

Image of table (I can't post pics) http://i.stack.imgur.com/dAO6j.png

I have been reviewing all the questions related to this topic and I have found some answers for applying grid span to the cells, but I couldn't find a real solution.

Here is the code I have from examples obtained from google and from this site:

    XWPFDocument document = new XWPFDocument();
    XWPFTable table = document.createTable(7, 2);

    fillTable(table);

    XWPFTableCell cellRow1 = table.getRow(0).getCell(0);
    XWPFTableCell cellRow2 = table.getRow(1).getCell(0);

    cellRow1.getCTTc().addNewTcPr();
    cellRow1.getCTTc().getTcPr().addNewGridSpan();
    cellRow1.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    cellRow2.getCTTc().addNewTcPr();
    cellRow2.getCTTc().getTcPr().addNewGridSpan();
    cellRow2.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    FileOutputStream out = new FileOutputStream("Table.docx");
    doc.write(out);
    out.close();

What I get from this code is the following:

enter image description here

I tried to remove the "extra" cells with table.getRow(0).removeCell(1); but it didn't work, am I doing something wrong?

like image 764
JuanDeLaMora Avatar asked Nov 30 '14 04:11

JuanDeLaMora


People also ask

How do I merge cells in a table into one cell in Word?

Select the cells that you want to merge. Under Table Tools, on the Layout tab, in the Merge group, click Merge Cells.

How do you merge cells in a table?

Merge cellsIn the table, drag the pointer across the cells that you want to merge. Click the Layout tab. In the Merge group, click Merge Cells.

How do I merge cells in Xssfworkbook?

Apache POI SS - HSSF XSSF - Merge Cells Sheet. addMergedRegion can be used to Merge Cells.


2 Answers

It seems xml has to be removed as well:

 XWPFTableCell removed = tableRow.getCell(idx);
 removed.getCTTc().newCursor().removeXml();
 tableRow.removeCell(idx);
like image 58
azis.mrazish Avatar answered Nov 14 '22 23:11

azis.mrazish


To merge horizontally/vertically you need to create 2 CTHMerge and use the setVal:

  • one for the cells that you will remain (STMerge.RESTART);
  • a second one for the merged cells (STMerge.CONTINUE);

a) example of a horizontal merge for a 2x2 table:

|___________|___________| --> |___________ ___________|
|___________|___________| --> |___________ ___________|

// First Row
CTHMerge hMerge = CTHMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);
table.getRow(1).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);

// Secound Row cell will be merged/"deleted"
CTHMerge hMerge1 = CTHMerge.Factory.newInstance();
hMerge.setVal(STMerge.CONTINUE);
table.getRow(0).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);

b) example of a vertical merge:

 // First Row
CTVMerge vmerge = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setVMerge(vmerge);
table.getRow(0).getCell(1).getCTTc().getTcPr().setVMerge(vmerge);

 // Secound Row cell will be merged 
CTVMerge vmerge1 = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.CONTINUE);
table.getRow(1).getCell(0).getCTTc().getTcPr().setVMerge(vmerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setVMerge(vmerge1);
like image 20
Jose1755 Avatar answered Nov 15 '22 01:11

Jose1755