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
This commit is contained in:
Yegor Kozlov 2012-02-14 07:02:54 +00:00
parent 2995695988
commit 1ed2118402
3 changed files with 78 additions and 11 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.8-beta6" date="2012-??-??"> <release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="add">52658 - support mergin table cells in XSLF</action>
<action dev="poi-developers" type="add">validate row number and column index in SXSSF when creating new rows / cells</action> <action dev="poi-developers" type="add">validate row number and column index in SXSSF when creating new rows / cells</action>
<action dev="poi-developers" type="fix">51498 - fixed evaluation of blank cells in COUNTIF</action> <action dev="poi-developers" type="fix">51498 - fixed evaluation of blank cells in COUNTIF</action>
<action dev="poi-developers" type="add">52576 - support changing external file references in HSSFWorkbook</action> <action dev="poi-developers" type="add">52576 - support changing external file references in HSSFWorkbook</action>

View File

@ -19,6 +19,13 @@
package org.apache.poi.xslf.usermodel; 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.POIXMLException;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
@ -26,7 +33,6 @@ import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl; 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.CTGraphicalObjectData;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable; 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.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual; 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 * Represents a table in a .pptx presentation
* *
@ -122,7 +122,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
cnv.setId(shapeId + 1); cnv.setId(shapeId + 1);
nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true); nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
nvGr.addNewNvPr(); nvGr.addNewNvPr();
frame.addNewXfrm(); frame.addNewXfrm();
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData(); CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor cursor = gr.newCursor(); XmlCursor cursor = gr.newCursor();
@ -135,4 +135,55 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
gr.setUri(TABLE_URI); gr.setUri(TABLE_URI);
return frame; return frame;
} }
/**
* Merge cells of a table
*/
public void mergeCells(int firstRow, int lastRow, int firstCol, int lastCol) {
if(firstRow > 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);
}
}
}
}
}
} }

View File

@ -19,6 +19,8 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Color;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties; 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.STPenAlignment;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal; import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import java.awt.Color;
/** /**
* Represents a cell of a table in a .pptx presentation * Represents a cell of a table in a .pptx presentation
* *
@ -83,7 +83,7 @@ public class XSLFTableCell extends XSLFTextShape {
pr.setMarL(Units.toEMU(margin)); pr.setMarL(Units.toEMU(margin));
} }
@Override @Override
public void setRightInset(double margin){ public void setRightInset(double margin){
CTTableCellProperties pr = getXmlObject().getTcPr(); 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]); 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_);
}
} }