mirror of https://github.com/apache/poi.git
Insert a new row in XSLFTable
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1875901 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a850690d7c
commit
c67d2605fd
|
@ -153,19 +153,41 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
|||
|
||||
public XSLFTableRow addRow(){
|
||||
CTTableRow tr = _table.addNewTr();
|
||||
return initializeRow(tr);
|
||||
}
|
||||
|
||||
private XSLFTableRow initializeRow(CTTableRow tr) {
|
||||
XSLFTableRow row = new XSLFTableRow(tr, this);
|
||||
// default height is 20 points
|
||||
row.setHeight(20.0);
|
||||
_rows.add(row);
|
||||
updateRowColIndexes();
|
||||
for (int i = 0; i < getNumberOfColumns(); i++) {
|
||||
row.addCell();
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new row at the given index.
|
||||
* @param rowIdx the row index.
|
||||
* @since POI 4.1.3
|
||||
*/
|
||||
public XSLFTableRow insertRow(int rowIdx) {
|
||||
if (getNumberOfRows() < rowIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot insert row at " + rowIdx + "; table has only " + getNumberOfRows() + "rows.");
|
||||
}
|
||||
CTTableRow tr = _table.insertNewTr(rowIdx);
|
||||
return initializeRow(tr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the row on the given index
|
||||
* @param rowIdx the row index
|
||||
*/
|
||||
public void removeRow(int rowIdx) {
|
||||
if (getNumberOfRows() < rowIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot remove row at " + rowIdx + "; table has only " + getNumberOfRows() + "rows.");
|
||||
}
|
||||
_table.removeTr(rowIdx);
|
||||
_rows.remove(rowIdx);
|
||||
updateRowColIndexes();
|
||||
|
@ -176,14 +198,13 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
|||
* @since POI 4.1.2
|
||||
*/
|
||||
public void addColumn() {
|
||||
long width = _table.getTblGrid().getGridColArray(_table.getTblGrid().sizeOfGridColArray() - 1).getW();
|
||||
long width = _table.getTblGrid().getGridColArray(getNumberOfColumns() - 1).getW();
|
||||
CTTableCol col = _table.getTblGrid().addNewGridCol();
|
||||
col.setW(width);
|
||||
for(XSLFTableRow row : _rows) {
|
||||
for (XSLFTableRow row : _rows) {
|
||||
XSLFTableCell cell = row.addCell();
|
||||
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
|
||||
}
|
||||
updateRowColIndexes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -192,17 +213,16 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
|||
* @since POI 4.1.2
|
||||
*/
|
||||
public void insertColumn(int colIdx) {
|
||||
if (_table.getTblGrid().sizeOfGridColArray() < colIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + "; table has only " + _table.getTblGrid().sizeOfGridColArray() + "columns.");
|
||||
if (getNumberOfColumns() < colIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + "; table has only " + getNumberOfColumns() + "columns.");
|
||||
}
|
||||
long width = _table.getTblGrid().getGridColArray(colIdx).getW();
|
||||
CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
|
||||
col.setW(width);
|
||||
for(XSLFTableRow row : _rows) {
|
||||
for (XSLFTableRow row : _rows) {
|
||||
XSLFTableCell cell = row.insertCell(colIdx);
|
||||
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
|
||||
}
|
||||
updateRowColIndexes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,11 +231,13 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
|||
* @since POI 4.1.2
|
||||
*/
|
||||
public void removeColumn(int colIdx) {
|
||||
if (getNumberOfColumns() < colIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot remove column at " + colIdx + "; table has only " + getNumberOfColumns() + "columns.");
|
||||
}
|
||||
_table.getTblGrid().removeGridCol(colIdx);
|
||||
for(XSLFTableRow row : _rows) {
|
||||
for (XSLFTableRow row : _rows) {
|
||||
row.removeCell(colIdx);
|
||||
}
|
||||
updateRowColIndexes();
|
||||
}
|
||||
|
||||
static CTGraphicalObjectFrame prototype(int shapeId){
|
||||
|
|
|
@ -105,6 +105,9 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
|
|||
* @since POI 4.1.2
|
||||
*/
|
||||
public void removeCell(int colIdx){
|
||||
if (_row.sizeOfTcArray() < colIdx) {
|
||||
throw new IndexOutOfBoundsException("Cannot remove cell at " + colIdx + "; row has only " + _row.sizeOfTcArray() + "columns.");
|
||||
}
|
||||
_row.removeTc(colIdx);
|
||||
_cells.remove(colIdx);
|
||||
_table.updateRowColIndexes();
|
||||
|
|
|
@ -55,7 +55,7 @@ public class TestXSLFTable {
|
|||
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
int rowIdx=1;
|
||||
while (rowIdx<data.length) {
|
||||
|
||||
XSLFSlide slide = ppt.createSlide();
|
||||
// a red bordered box in the background, to show/verify the table dimensions
|
||||
XSLFAutoShape as = slide.createAutoShape();
|
||||
|
@ -90,25 +90,31 @@ public class TestXSLFTable {
|
|||
tr.setFontFamily("Arial");
|
||||
}
|
||||
|
||||
|
||||
while (rowIdx<data.length) {
|
||||
row = tab.addRow();
|
||||
for (int col=0; col<data[rowIdx].length; col++) {
|
||||
XSLFTextRun tr = row.addCell().setText(data[rowIdx][col]);
|
||||
XSLFTextRun tr = tab.getCell(rowIdx, col).setText(data[rowIdx][col]);
|
||||
tr.setFontSize(15.);
|
||||
tr.setFontFamily("Arial");
|
||||
}
|
||||
row = tab.insertRow(rowIdx);
|
||||
for (int col=0; col<data[rowIdx].length; col++) {
|
||||
XSLFTextRun tr = tab
|
||||
.getCell(rowIdx, col)
|
||||
.setText(
|
||||
data[rowIdx][col]);
|
||||
tr.setFontSize(12.);
|
||||
tr.setFontFamily("Arial");
|
||||
}
|
||||
tab.updateCellAnchor();
|
||||
if (tab.getAnchor().getHeight() > maxHeight) {
|
||||
tab.removeRow(rowIdx-startRow);
|
||||
break;
|
||||
}
|
||||
rowIdx++;
|
||||
rowIdx += 2;
|
||||
}
|
||||
|
||||
tab.updateCellAnchor();
|
||||
as.setAnchor(tab.getAnchor());
|
||||
}
|
||||
|
||||
File fileOut = TempFile.createTempFile("tabtest", ".pptx");
|
||||
try (FileOutputStream fos = new FileOutputStream(fileOut)) {
|
||||
|
|
Loading…
Reference in New Issue