6

how can I merge cells over 2 or more rows (row span) using XWPFTable in POI in Java?

I know how merge cells in one row but i have no idea how do it and i don't find any example of this.

thanks in advance.

Błażej
  • 151
  • 1
  • 7
  • You mean you ALREADY hava a table with N rows and M columns, and you want to merge some cells, say "B5" and "B6" (i know you mean Word, not Excel, but just to describe the cell coordinates in the same way) – DenisFLASH Jul 24 '14 at 11:42
  • Or, you want to create a table from scratch? – DenisFLASH Jul 24 '14 at 11:43
  • it doesn't really matter, i can do both create or update table. Yes, i want merge cells like B5 and B6. – Błażej Jul 25 '14 at 18:14
  • 1
    why i asked it is that if you want to build the table from scratch, then you can just build your table cell-by-cell and when you want your BIG CELL to be created, you can try inserting even a new TABLE inside the old one. Does it interest you that i spend some time trying it and then will post the code? But if you have a strong constraint to update the existing table, than i don't know how to do this row span – DenisFLASH Jul 26 '14 at 07:14
  • thanks, it is good point to start with. On the other hand, I find the method which look like helpfull, but i have no idea how to use it table.getRow(0).getCell(0).getCTTc().getTcPr().setVMerge(CTVMerge arg); – Błażej Jul 29 '14 at 06:28
  • found it! you have found the first half of the answer. Teamwork =) – DenisFLASH Jul 29 '14 at 13:31
  • Please see if this is useful: http://stackoverflow.com/questions/27209863/apache-poi-merge-cells-from-a-table-in-a-word-document -- Regards, RRPV. – Raghavendra Rao PV Aug 03 '15 at 03:44

1 Answers1

12

Found it! Thanks to your own comment about getCTTc().getTcPr()... and this site, here is a little program to test vertical merge:

import java.io.FileOutputStream;
import java.io.IOException;

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;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;


public class VerticalMerge {

    public static void main(String[] args) {

        int rows = 5;
        int cols = 5;

        XWPFDocument document = new XWPFDocument();
        XWPFTable table = document.createTable(rows, cols);

        fillTable(table);

        mergeCellsVertically(table, 3, 1, 3);

        try {
            FileOutputStream out = new FileOutputStream("vertical merge.docx");
            document.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void fillTable(XWPFTable table) {
        for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
            XWPFTableRow row = table.getRow(rowIndex);

            for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {
                XWPFTableCell cell = row.getCell(colIndex);
                cell.addParagraph().createRun().setText(" cell " + rowIndex + colIndex + " ");
            }
        }
    }

    private static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {

        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {

            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);

            if ( rowIndex == fromRow ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

}

For the moment, it doesn't concatenate text in merged cells, but i have an idea and will update this answer soon. Good luck!

DenisFLASH
  • 734
  • 1
  • 9
  • 14
  • This is it. Thanks a lot :). It is not necessary to concatenate text in merged cells what is more for me it event should not do this to be consistent with POI HSSF(XSSF) which simply override merged cells. – Błażej Jul 30 '14 at 07:30
  • Ok, got it. Thanks to you for a nice question and a hint, without which i wouldn't have found an answer. I also tried to merge some blocks of cells, say, 2x3 cells, but the effect was as if i used two separate vertical merge over 1x3 vertical block. Probably, to delete the "walls" between those blocks, we have to do a **horizontal** merge after. Well, depending on your task, you can test some "2D merge", etc. Good luck, @blazej! – DenisFLASH Jul 30 '14 at 08:25
  • Hi, Excellent solution! It works wonderfully. I want to add that, the way to handle merging the text in each cell within a vertical merge range, is to loop through each of the cells to be merged, then loop through all paragraphs and runs and grab the text from each run. – Dan Torrey Jan 09 '15 at 19:35