From 073c3e278fa5355994bb3e9a420797364fa03b2d Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Tue, 14 Feb 2012 07:02:54 +0000 Subject: [PATCH] Bugzilla 52658: support mergin table cells in XSLF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1243793 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xslf/usermodel/XSLFTable.java | 67 ++++++++++++++++--- .../poi/xslf/usermodel/XSLFTableCell.java | 21 +++++- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e4a391cd11..e4022e7a64 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52658 - support mergin table cells in XSLF validate row number and column index in SXSSF when creating new rows / cells 51498 - fixed evaluation of blank cells in COUNTIF 52576 - support changing external file references in HSSFWorkbook diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java index 5f9713e7ee..b8b2690509 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java @@ -19,6 +19,13 @@ package org.apache.poi.xslf.usermodel; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import javax.xml.namespace.QName; + import org.apache.poi.POIXMLException; import org.apache.poi.util.Internal; import org.apache.poi.util.Units; @@ -26,7 +33,6 @@ import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl; -import org.openxmlformats.schemas.drawingml.x2006.chart.CTDTable; import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTTable; @@ -34,12 +40,6 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow; import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame; import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual; -import javax.xml.namespace.QName; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - /** * Represents a table in a .pptx presentation * @@ -122,7 +122,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable lastRow) { + throw new IllegalArgumentException( + "Cannot merge, first row > last row : " + + firstRow + " > " + lastRow + ); + } + + if(firstCol > lastCol) { + throw new IllegalArgumentException( + "Cannot merge, first column > last column : " + + firstCol + " > " + lastCol + ); + } + + int rowSpan = (lastRow - firstRow) + 1; + boolean mergeRowRequired = rowSpan > 1; + + int colSpan = (lastCol - firstCol) + 1; + boolean mergeColumnRequired = colSpan > 1; + + for(int i = firstRow; i <= lastRow; i++) { + + XSLFTableRow row = _rows.get(i); + + for(int colPos = firstCol; colPos <= lastCol; colPos++) { + + XSLFTableCell cell = row.getCells().get(colPos); + + if(mergeRowRequired) { + if(i == firstRow) { + cell.setRowSpan(rowSpan); + } else { + cell.setVMerge(true); + } + } + if(mergeColumnRequired) { + if(colPos == firstCol) { + cell.setGridSpan(colSpan); + } else { + cell.setHMerge(true); + } + } + } + } + } } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java index f3d4400aa0..df63212af6 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java @@ -19,6 +19,8 @@ package org.apache.poi.xslf.usermodel; +import java.awt.Color; + import org.apache.poi.util.Units; import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties; @@ -35,8 +37,6 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth; import org.openxmlformats.schemas.drawingml.x2006.main.STPenAlignment; import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal; -import java.awt.Color; - /** * Represents a cell of a table in a .pptx presentation * @@ -83,7 +83,7 @@ public class XSLFTableCell extends XSLFTextShape { pr.setMarL(Units.toEMU(margin)); } - + @Override public void setRightInset(double margin){ CTTableCellProperties pr = getXmlObject().getTcPr(); @@ -284,4 +284,19 @@ public class XSLFTableCell extends XSLFTextShape { return new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]); } + void setGridSpan(int gridSpan_) { + getXmlObject().setGridSpan(gridSpan_); + } + + void setRowSpan(int rowSpan_) { + getXmlObject().setRowSpan(rowSpan_); + } + + void setHMerge(boolean merge_) { + getXmlObject().setHMerge(merge_); + } + + void setVMerge(boolean merge_) { + getXmlObject().setVMerge(merge_); + } } -- 2.39.5