zoukankan      html  css  js  c++  java
  • 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文档结构,tc表示一个单元格,其中,数据存放在p下面的r标签中。
    因此,只需要添加一段,就可以达到我们想要的效果:cell.getCTTc().getPArray(0).removeR(0);

    注意:getPArray(0)指的是第1个p标签,实际上p标签的数量不是固定的,需要注意自己文档下的p标签个数,按照实际情况进行填写。

            <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>

    最终代码

        /**
         * 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);
                }
            }
        }
  • 相关阅读:
    jQuery的鼠标悬停时放大图片的效果
    判断一个字符串在另一个字符串中出现的次数
    .net中几个经常用到的字符串的截取
    云服务器 ECS CentOS 7 下重启 sshd 服务操作方法
    博客生涯开始咯
    Json 转 Map 的几种方式
    JS 折线图
    CSS样式优先级
    Arrays.asList 数组转集合 java.lang.UnsupportedOperationException错误
    Netty 笔记
  • 原文地址:https://www.cnblogs.com/chenss15060100790/p/13906954.html
Copyright © 2011-2022 走看看