POI-Word合并单元格,拖动之后还原问题

Word文件合并单元格,在网络上,很容易找到下面这个函数,但是,这个函数有一个 Bug,在生成 Word 之后,如果拖动单元格,合并的单元格又会重新还原。

    /**
     * word单元格列合并
     *
     * @param table     表格
     * @param row       合并列所在行
     * @param startCell 开始列
     * @param endCell   结束列
     * @date 2020年4月8日 下午4:43:54
     */
    public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
        for (int i = startCell; i <= endCell; i++) {
            XWPFTableCell cell = table.getRow(row).getCell(i);
            if (i == startCell) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

解决办法也很简单,合并单元格之后,清空单元格中的数据即可(只保留第一个)。
下面是 Word 文档结构(转换成 xml 之后即可查看内容),tc 表示一个单元格,其中,数据存放在 p 下面的r标签中。
因此,只需要添加一段,就可以达到我们想要的效果:cell.getCTTc().getPArray(0).removeR(0);

        <w:tc>
                <w:tcPr>
                    <w:tcW w:w="0" w:type="auto"/>
                </w:tcPr>
                <w:p w14:paraId="3049E24E" w14:textId="77777777" w:rsidR="00B21DED" w:rsidRDefault="00ED7147">
                    <w:pPr>
                        <w:jc w:val="center"/>
                    </w:pPr>
                    <w:r>
                        <w:t>杭州市</w:t>
                    </w:r>
                </w:p>
            </w:tc>

最终代码

注意:下面这个代码不是最终版本,cell.getCTTc().getPArray(0) 指的是第 1 个 p 标签,实际上 p 标签的数量不是固定的,按照实际情况进行填写。

    /**
     * word单元格列合并
     *
     * @param table     表格
     * @param row       合并列所在行
     * @param startCell 开始列
     * @param endCell   结束列
     * @date 2020年4月8日 下午4:43:54
     */
    public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
        for (int i = startCell; i <= endCell; i++) {
            XWPFTableCell cell = table.getRow(row).getCell(i);
            if (i == startCell) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                cell.getCTTc().getPArray(0).removeR(0);
            }
        }
    }

    /**
     * word单元格行合并
     *
     * @param table    表格
     * @param col      合并行所在列
     * @param startRow 开始行
     * @param endRow   结束行
     * @date 2020年4月8日 下午4:46:18
     */
    public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {
        for (int i = startRow; i <= endRow; i++) {
            XWPFTableCell cell = table.getRow(i).getCell(col);
            if (i == startRow) {
                // 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);
                cell.getCTTc().getPArray(0).removeR(0);
            }
        }
    }

 

posted on 2020-10-31 17:52  疯狂的妞妞  阅读(624)  评论(0编辑  收藏  举报

导航