Discussion:
Delete Row from XSLFTable
Andreas Beeker
2018-05-11 14:30:44 UTC
Permalink
What is the proper way to delete a row from an XSLFTable?
If you aren't modify the table instance further, you can use getCTTable().removeTr(index):

// prepare some data

ByteArrayOutputStream bos = new ByteArrayOutputStream(50000); try (XMLSlideShow ppt = new XMLSlideShow()) {
XSLFTable tab = ppt.createSlide().createTable(5, 5); for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
tab.getCell(y, x).setText(y + "" + x); }
}
ppt.write(bos); }

// remove a row

try (XMLSlideShow ppt = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray())); FileOutputStream fos = new FileOutputStream("test.pptx")) {
XSLFTable tab = (XSLFTable)ppt.getSlides().get(0).getShapes().get(0); tab.getCTTable().removeTr(3); ppt.write(fos); }


Andi
j***@gmail.com
2018-05-14 14:52:55 UTC
Permalink
Post by Andreas Beeker
What is the proper way to delete a row from an XSLFTable?
// prepare some data
ByteArrayOutputStream bos = new ByteArrayOutputStream(50000); try (XMLSlideShow ppt = new XMLSlideShow()) {
XSLFTable tab = ppt.createSlide().createTable(5, 5); for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
tab.getCell(y, x).setText(y + "" + x); }
}
ppt.write(bos); }
// remove a row
try (XMLSlideShow ppt = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray())); FileOutputStream fos = new FileOutputStream("test.pptx")) {
XSLFTable tab = (XSLFTable)ppt.getSlides().get(0).getShapes().get(0); tab.getCTTable().removeTr(3); ppt.write(fos); }
Andi
getCTTable().removeTr(index) results in the same exception being thrown. I need to delete 100+ table rows in a single powerpoint, many of them in the same table. It isn't practical for me to save the file after every row delete


---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@poi.apache.org
For additional commands, e-mail: user-***@poi.apache.org
Mark Murphy
2018-05-14 15:31:04 UTC
Permalink
I'm thinking that this is a general rather than specific approach. Whether
you remove one or many rows, that would happen in the place called //
remove a row.
Post by Andreas Beeker
Post by Andreas Beeker
What is the proper way to delete a row from an XSLFTable?
If you aren't modify the table instance further, you can use
// prepare some data
ByteArrayOutputStream bos = new ByteArrayOutputStream(50000); try
(XMLSlideShow ppt = new XMLSlideShow()) {
Post by Andreas Beeker
XSLFTable tab = ppt.createSlide().createTable(5, 5); for (int y =
0; y < 5; y++) {
Post by Andreas Beeker
for (int x = 0; x < 5; x++) {
tab.getCell(y, x).setText(y + "" + x); }
}
ppt.write(bos); }
// remove a row
try (XMLSlideShow ppt = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray()));
FileOutputStream fos = new FileOutputStream("test.pptx")) {
Post by Andreas Beeker
XSLFTable tab = (XSLFTable)ppt.getSlides().get(0).getShapes().get(0);
tab.getCTTable().removeTr(3); ppt.write(fos); }
Post by Andreas Beeker
Andi
getCTTable().removeTr(index) results in the same exception being thrown. I
need to delete 100+ table rows in a single powerpoint, many of them in the
same table. It isn't practical for me to save the file after every row
delete
---------------------------------------------------------------------
Andreas Beeker
2018-05-14 17:06:29 UTC
Permalink
Mark is correct ... I've only added the create-slideshow part, to verify, that it is working when opening an existing file, i.e. otherwise [when creating a new file] the use case of removing rows wouldn't make sense anyway.

So when getCTTable().removeTr(index) results in the same exception being thrown, you might want to provide your test file by bugzilla entry or private mail (...  whatever suits you)

Andi
Post by Mark Murphy
I'm thinking that this is a general rather than specific approach. Whether
you remove one or many rows, that would happen in the place called //
remove a row.
Loading...