POI 获取docx类型文档中表格的rowSapn(跨行数)和colSpan(跨列数)
POI 获取colSapn(跨列数)rowSapn(跨行数)和思路
POI最外层的封装并没有提供获取colSpan和rowSpan的方法,但是在ooxml包里则提供了对于docx文档底层open xml的操作的封装,利用这个包里提供的类可以获取到docx文档大部分属性。
一:获取colSpan
- 获取ooxml包封装的单元格类:
CTTc ctTc = tableCell.getCTTc()
- 获取单元格属性:
CTTcPr ctTcPr = ctTc.getTcPr()
- 获取属性中的gridSpan属性:
CTDecimalNumber gridSpan = ctTcPr.getGridSpan()
- 获取gridSpan属性的值:
int colSpan = gridSpan.getVal().intValue();
二:获取rowSpan:
- 获取ooxml包封装的单元格类:
CTTc ctTc = tableCell.getCTTc()
- 获取单元格属性:
CTTcPr ctTcPr = ctTc.getTcPr()
-
获取单元格属性中VMerge属性
CTVMerge ctvMerge = ctTcPr.getVMerge()
- 获取VMerge属性的值
STMerge.Enum vMergeEnum = ctvMerge.getVal()
- 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标记的单元格结束