POI 获取docx类型文档中表格的rowSapn(跨行数)和colSpan(跨列数)

POI 获取colSapn(跨列数)rowSapn(跨行数)和思路
  POI最外层的封装并没有提供获取colSpan和rowSpan的方法,但是在ooxml包里则提供了对于docx文档底层open xml的操作的封装,利用这个包里提供的类可以获取到docx文档大部分属性。

一:获取colSpan

  1. 获取ooxml包封装的单元格类:
    CTTc ctTc =  tableCell.getCTTc()
  2. 获取单元格属性:
    CTTcPr ctTcPr = ctTc.getTcPr()
  3. 获取属性中的gridSpan属性:
    CTDecimalNumber gridSpan = ctTcPr.getGridSpan()
  4. 获取gridSpan属性的值:
    int colSpan = gridSpan.getVal().intValue();

二:获取rowSpan:

  1. 获取ooxml包封装的单元格类:
    CTTc ctTc =  tableCell.getCTTc()
  2. 获取单元格属性:
    CTTcPr ctTcPr = ctTc.getTcPr()
  3.  获取单元格属性中VMerge属性

    CTVMerge ctvMerge = ctTcPr.getVMerge()
  4. 获取VMerge属性的值
    STMerge.Enum vMergeEnum = ctvMerge.getVal()
  5. VMerge属性的值是一个枚举类,定义如下:
    public static final class Enum extends StringEnumAbstractBase {
            static final int INT_CONTINUE = 1;
            static final int INT_RESTART = 2;
            public static final Table table = new Table(new STMerge.Enum[]{new STMerge.Enum("continue", 1), new STMerge.Enum("restart", 2)});
            private static final long serialVersionUID = 1L;
    
            public static STMerge.Enum forString(String var0) {
                return (STMerge.Enum)table.forString(var0);
            }
    
            public static STMerge.Enum forInt(int var0) {
                return (STMerge.Enum)table.forInt(var0);
            }
    
            private Enum(String var1, int var2) {
                super(var1, var2);
            }
    
            private Object readResolve() {
                return forInt(this.intValue());
            }
        }
    

      若为RESTART则标志开始行合并,为CONTINUE则标志继续行合并,直到遇到没有CONTINUE标记或者有RESTART标记的单元格结束

  

posted @ 2022-01-19 15:52  会飞的joy  阅读(758)  评论(1编辑  收藏  举报