How to colspan a table in word with APACHE POI

前端 未结 3 748
后悔当初
后悔当初 2020-12-18 13:51

I have a code, but it\'s for rowspan a table. Can you help me to convert this to colspan code?

private static void mergeCellVertically(XWPFTable table, int c         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-18 14:15

    Axel Richter answer is very complete. But there is a problem if you don't work CTTcPr and other XML keys, and mix with POI API's.

    You have to use

            // Cells which join (merge) the first one, must be removed
            int sizeOfTc = currentRow.getCtRow().sizeOfTcArray();
            for(int colIndex = toCol; colIndex > fromCol; colIndex--) {
                if (colIndex < sizeOfTc) currentRow.getCtRow().removeTc(colIndex);
                currentRow.removeCell(colIndex);
            }
    

    because there is a bug on Apache POI. The removeCell() method doesn't remove the Tc from the row. And if you use removeTc(), it doesn't remove the cell from the internal POI's list. You have to use both.

提交回复
热议问题