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(){
|
public XSLFTableRow addRow(){
|
||||||
CTTableRow tr = _table.addNewTr();
|
CTTableRow tr = _table.addNewTr();
|
||||||
|
return initializeRow(tr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private XSLFTableRow initializeRow(CTTableRow tr) {
|
||||||
XSLFTableRow row = new XSLFTableRow(tr, this);
|
XSLFTableRow row = new XSLFTableRow(tr, this);
|
||||||
// default height is 20 points
|
// default height is 20 points
|
||||||
row.setHeight(20.0);
|
row.setHeight(20.0);
|
||||||
_rows.add(row);
|
_rows.add(row);
|
||||||
updateRowColIndexes();
|
for (int i = 0; i < getNumberOfColumns(); i++) {
|
||||||
|
row.addCell();
|
||||||
|
}
|
||||||
return row;
|
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
|
* Remove the row on the given index
|
||||||
* @param rowIdx the row index
|
* @param rowIdx the row index
|
||||||
*/
|
*/
|
||||||
public void removeRow(int rowIdx) {
|
public void removeRow(int rowIdx) {
|
||||||
|
if (getNumberOfRows() < rowIdx) {
|
||||||
|
throw new IndexOutOfBoundsException("Cannot remove row at " + rowIdx + "; table has only " + getNumberOfRows() + "rows.");
|
||||||
|
}
|
||||||
_table.removeTr(rowIdx);
|
_table.removeTr(rowIdx);
|
||||||
_rows.remove(rowIdx);
|
_rows.remove(rowIdx);
|
||||||
updateRowColIndexes();
|
updateRowColIndexes();
|
||||||
|
@ -176,14 +198,13 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
||||||
* @since POI 4.1.2
|
* @since POI 4.1.2
|
||||||
*/
|
*/
|
||||||
public void addColumn() {
|
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();
|
CTTableCol col = _table.getTblGrid().addNewGridCol();
|
||||||
col.setW(width);
|
col.setW(width);
|
||||||
for(XSLFTableRow row : _rows) {
|
for (XSLFTableRow row : _rows) {
|
||||||
XSLFTableCell cell = row.addCell();
|
XSLFTableCell cell = row.addCell();
|
||||||
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
|
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
|
* @since POI 4.1.2
|
||||||
*/
|
*/
|
||||||
public void insertColumn(int colIdx) {
|
public void insertColumn(int colIdx) {
|
||||||
if (_table.getTblGrid().sizeOfGridColArray() < colIdx) {
|
if (getNumberOfColumns() < colIdx) {
|
||||||
throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + "; table has only " + _table.getTblGrid().sizeOfGridColArray() + "columns.");
|
throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + "; table has only " + getNumberOfColumns() + "columns.");
|
||||||
}
|
}
|
||||||
long width = _table.getTblGrid().getGridColArray(colIdx).getW();
|
long width = _table.getTblGrid().getGridColArray(colIdx).getW();
|
||||||
CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
|
CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
|
||||||
col.setW(width);
|
col.setW(width);
|
||||||
for(XSLFTableRow row : _rows) {
|
for (XSLFTableRow row : _rows) {
|
||||||
XSLFTableCell cell = row.insertCell(colIdx);
|
XSLFTableCell cell = row.insertCell(colIdx);
|
||||||
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
|
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
|
* @since POI 4.1.2
|
||||||
*/
|
*/
|
||||||
public void removeColumn(int colIdx) {
|
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);
|
_table.getTblGrid().removeGridCol(colIdx);
|
||||||
for(XSLFTableRow row : _rows) {
|
for (XSLFTableRow row : _rows) {
|
||||||
row.removeCell(colIdx);
|
row.removeCell(colIdx);
|
||||||
}
|
}
|
||||||
updateRowColIndexes();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static CTGraphicalObjectFrame prototype(int shapeId){
|
static CTGraphicalObjectFrame prototype(int shapeId){
|
||||||
|
|
|
@ -105,6 +105,9 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
|
||||||
* @since POI 4.1.2
|
* @since POI 4.1.2
|
||||||
*/
|
*/
|
||||||
public void removeCell(int colIdx){
|
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);
|
_row.removeTc(colIdx);
|
||||||
_cells.remove(colIdx);
|
_cells.remove(colIdx);
|
||||||
_table.updateRowColIndexes();
|
_table.updateRowColIndexes();
|
||||||
|
|
|
@ -55,61 +55,67 @@ public class TestXSLFTable {
|
||||||
|
|
||||||
XMLSlideShow ppt = new XMLSlideShow();
|
XMLSlideShow ppt = new XMLSlideShow();
|
||||||
int rowIdx=1;
|
int rowIdx=1;
|
||||||
|
|
||||||
|
XSLFSlide slide = ppt.createSlide();
|
||||||
|
// a red bordered box in the background, to show/verify the table dimensions
|
||||||
|
XSLFAutoShape as = slide.createAutoShape();
|
||||||
|
as.setShapeType(ShapeType.RECT);
|
||||||
|
as.setStrokeStyle(Color.RED, 2., StrokeStyle.LineDash.LG_DASH);
|
||||||
|
|
||||||
|
XSLFTable tab = slide.createTable(1, data[0].length);
|
||||||
|
tab.setAnchor(new Rectangle2D.Double(50,50,0,0));
|
||||||
|
tab.setColumnWidth(0, 60);
|
||||||
|
tab.setColumnWidth(1, 60);
|
||||||
|
tab.setColumnWidth(2, 60);
|
||||||
|
|
||||||
|
tab.insertColumn(0);
|
||||||
|
assertEquals(tab.getColumnWidth(1), tab.getColumnWidth(0), 0.00001);
|
||||||
|
assertNotNull(tab.getCell(0, 0).getTextBody());
|
||||||
|
tab.addColumn();
|
||||||
|
XSLFTableCell cell = tab.getCell(0, data[0].length + 1);
|
||||||
|
assertEquals(1, cell.getTextBody().getParagraphs().size());
|
||||||
|
assertEquals("", cell.getTextBody().getParagraph(0).getText());
|
||||||
|
assertEquals(tab.getColumnWidth(tab.getNumberOfColumns() - 2), tab.getColumnWidth(tab.getNumberOfColumns() - 1), 0.00001);
|
||||||
|
assertNotNull(tab.getCell(0, tab.getNumberOfColumns() - 1).getTextBody());
|
||||||
|
tab.removeColumn(0);
|
||||||
|
tab.removeColumn(tab.getNumberOfColumns() - 1);
|
||||||
|
assertEquals(data[0].length, tab.getNumberOfColumns());
|
||||||
|
|
||||||
|
int startRow = rowIdx-1;
|
||||||
|
|
||||||
|
XSLFTableRow row = tab.getRows().get(0);
|
||||||
|
for (int colIdx=0; colIdx<data[0].length; colIdx++) {
|
||||||
|
XSLFTextRun tr = row.getCells().get(colIdx).setText(data[0][colIdx]);
|
||||||
|
tr.setFontSize(20.);
|
||||||
|
tr.setFontFamily("Arial");
|
||||||
|
}
|
||||||
|
|
||||||
while (rowIdx<data.length) {
|
while (rowIdx<data.length) {
|
||||||
XSLFSlide slide = ppt.createSlide();
|
row = tab.addRow();
|
||||||
// a red bordered box in the background, to show/verify the table dimensions
|
for (int col=0; col<data[rowIdx].length; col++) {
|
||||||
XSLFAutoShape as = slide.createAutoShape();
|
XSLFTextRun tr = tab.getCell(rowIdx, col).setText(data[rowIdx][col]);
|
||||||
as.setShapeType(ShapeType.RECT);
|
tr.setFontSize(15.);
|
||||||
as.setStrokeStyle(Color.RED, 2., StrokeStyle.LineDash.LG_DASH);
|
|
||||||
|
|
||||||
XSLFTable tab = slide.createTable(1, data[0].length);
|
|
||||||
tab.setAnchor(new Rectangle2D.Double(50,50,0,0));
|
|
||||||
tab.setColumnWidth(0, 60);
|
|
||||||
tab.setColumnWidth(1, 60);
|
|
||||||
tab.setColumnWidth(2, 60);
|
|
||||||
|
|
||||||
tab.insertColumn(0);
|
|
||||||
assertEquals(tab.getColumnWidth(1), tab.getColumnWidth(0), 0.00001);
|
|
||||||
assertNotNull(tab.getCell(0, 0).getTextBody());
|
|
||||||
tab.addColumn();
|
|
||||||
XSLFTableCell cell = tab.getCell(0, data[0].length + 1);
|
|
||||||
assertEquals(1, cell.getTextBody().getParagraphs().size());
|
|
||||||
assertEquals("", cell.getTextBody().getParagraph(0).getText());
|
|
||||||
assertEquals(tab.getColumnWidth(tab.getNumberOfColumns() - 2), tab.getColumnWidth(tab.getNumberOfColumns() - 1), 0.00001);
|
|
||||||
assertNotNull(tab.getCell(0, tab.getNumberOfColumns() - 1).getTextBody());
|
|
||||||
tab.removeColumn(0);
|
|
||||||
tab.removeColumn(tab.getNumberOfColumns() - 1);
|
|
||||||
assertEquals(data[0].length, tab.getNumberOfColumns());
|
|
||||||
|
|
||||||
int startRow = rowIdx-1;
|
|
||||||
|
|
||||||
XSLFTableRow row = tab.getRows().get(0);
|
|
||||||
for (int colIdx=0; colIdx<data[0].length; colIdx++) {
|
|
||||||
XSLFTextRun tr = row.getCells().get(colIdx).setText(data[0][colIdx]);
|
|
||||||
tr.setFontSize(20.);
|
|
||||||
tr.setFontFamily("Arial");
|
tr.setFontFamily("Arial");
|
||||||
}
|
}
|
||||||
|
row = tab.insertRow(rowIdx);
|
||||||
|
for (int col=0; col<data[rowIdx].length; col++) {
|
||||||
while (rowIdx<data.length) {
|
XSLFTextRun tr = tab
|
||||||
row = tab.addRow();
|
.getCell(rowIdx, col)
|
||||||
for (int col=0; col<data[rowIdx].length; col++) {
|
.setText(
|
||||||
XSLFTextRun tr = row.addCell().setText(data[rowIdx][col]);
|
data[rowIdx][col]);
|
||||||
tr.setFontSize(15.);
|
tr.setFontSize(12.);
|
||||||
tr.setFontFamily("Arial");
|
tr.setFontFamily("Arial");
|
||||||
}
|
|
||||||
tab.updateCellAnchor();
|
|
||||||
if (tab.getAnchor().getHeight() > maxHeight) {
|
|
||||||
tab.removeRow(rowIdx-startRow);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
rowIdx++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tab.updateCellAnchor();
|
tab.updateCellAnchor();
|
||||||
as.setAnchor(tab.getAnchor());
|
if (tab.getAnchor().getHeight() > maxHeight) {
|
||||||
|
tab.removeRow(rowIdx-startRow);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rowIdx += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
as.setAnchor(tab.getAnchor());
|
||||||
|
|
||||||
File fileOut = TempFile.createTempFile("tabtest", ".pptx");
|
File fileOut = TempFile.createTempFile("tabtest", ".pptx");
|
||||||
try (FileOutputStream fos = new FileOutputStream(fileOut)) {
|
try (FileOutputStream fos = new FileOutputStream(fileOut)) {
|
||||||
ppt.write(fos);
|
ppt.write(fos);
|
||||||
|
|
Loading…
Reference in New Issue