How to Split Cells in Word Table ( by Apache POI)
<p>How to correctly split row in the table that the columns would have a different width.
I generate a Word document through Java Apache POI. The aim to obtain the following table <a href="http://i.stack.imgur.com/dmYN9.png" rel="nofollow">correct output table format</a> (where the columns have different widths)</p>

<pre><code>import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.io.FileOutputStream;
import java.math.BigInteger;

public class Main {

public static void main(String[] args) {

    try {

        XWPFDocument docx = new XWPFDocument();

        XWPFParagraph paragraph = docx.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        paragraph.createRun().setText("Header");

        XWPFTable table1 = createTable(docx, 1, 3);
        tableParagraph(table1.getRow(0).getCell(0), "One");
        widthCellsAcrossRow(table1, 0, 0, 1);
        addGridSpan(table1, 0, 1, 2);
        table1.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        tableParagraph(table1.getRow(0).getCell(1), "Paragraph");

        XWPFTable table2 = createTable(docx, 1, 3);
        addGridSpan(table2, 0, 0, 3);
        table2.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        table2.getRow(0).getCell(1).getCTTc().newCursor().removeXml();

        XWPFTable table3 = createTable(docx, 1, 3);
        tableParagraph(table3.getRow(0).getCell(0), "One Two");
        addGridSpan(table3, 0, 1, 2);
        table3.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        tableParagraph(table3.getRow(0).getCell(1), "Paragraph");

        XWPFTable table4 = createTable(docx, 1, 3);
        addGridSpan(table4, 0, 0, 3);
        table4.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        table4.getRow(0).getCell(1).getCTTc().newCursor().removeXml();

        XWPFTable table5 = createTable(docx, 1, 3);
        tableParagraph(table5.getRow(0).getCell(0), "One Two Three");
        addGridSpan(table5, 0, 1, 2);
        table5.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        tableParagraph(table5.getRow(0).getCell(1), "Paragraph");

        XWPFTable table6 = createTable(docx, 1, 3);
        addGridSpan(table6, 0, 0, 3);
        table6.getRow(0).getCell(2).getCTTc().newCursor().removeXml();
        table6.getRow(0).getCell(1).getCTTc().newCursor().removeXml();

        XWPFTable table7 = createTable(docx, 1, 3);
        tableParagraph(table7.getRow(0).getCell(0), "One");
        tableParagraph(table7.getRow(0).getCell(1), "Two");
        tableParagraph(table7.getRow(0).getCell(2), "Three");

        FileOutputStream outStream = new FileOutputStream("SplitTable.docx");
        docx.write(outStream);
        outStream.close();


    } catch (Exception e) {
        System.out.println(e);
    }
}

private static void widthCellsAcrossRow(XWPFTable table, int rowNum, int colNum, int width) {
    XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
    if (cell.getCTTc().getTcPr() == null)
        cell.getCTTc().addNewTcPr();
    if (cell.getCTTc().getTcPr().getTcW() == null)
        cell.getCTTc().getTcPr().addNewTcW();
    cell.getCTTc().getTcPr().getTcW().setW(BigInteger.valueOf((long) width));
}

private static void addGridSpan(XWPFTable table, int rowNum, int colNum, int val) throws Exception {
    XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
    cell.getCTTc().addNewTcPr();
    cell.getCTTc().getTcPr().addNewGridSpan();
    cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(val));

}

private static XWPFTable createTable(XWPFDocument docx, int rows, int cols) {
    XWPFTable tbl;
    if (rows != 0 &amp;&amp; cols != 0)
        tbl = docx.createTable(rows, cols);
    else
        tbl = docx.createTable();

    CTTbl table = tbl.getCTTbl();
    CTTblPr pr = table.getTblPr();
    CTTblWidth tableWidth = pr.getTblW();
    tableWidth.setW(BigInteger.valueOf(5000));
    tableWidth.setType(STTblWidth.PCT);
    pr.setTblW(tableWidth);

    table.setTblPr(pr);
    return tbl;
}

private static XWPFParagraph tableParagraph(XWPFTableCell cell, String text) throws Exception {
    XWPFParagraph paragraph = cell.addParagraph();
    cell.removeParagraph(0);
    paragraph.createRun().setText(text);
    return paragraph;
}
}
</code></pre>

<p>My code generates a table with a fixed column width. <a href="http://i.stack.imgur.com/kn3OP.png" rel="nofollow">now that I have</a> Where am I wrong? What method can I use?</p>