Great - that's it !
The following piece of code does the trick:
OPCPackage pkg = workbook.getPackage();
for (XSSFTable table : sheet.getTables()) {
workbook.removePartRelation(table, true);
pkg.removePart(table.getPackagePart());
}
thank you!!
Christian
Am 29.02.2016 um 20:53 schrieb Dominik Stadler:
Hi,
I think you would use the XSSFWorkbook.getPackage() to get the OPCPackage,
which holds all the parts of the workbook and has things like
getPartsByRelationshipType() and removePart() which should make it possible
to not only remove the relationships, but also the parts themselves.
I hope this does what you are looking for!
Thanks... Dominik.
On Mon, Feb 29, 2016 at 4:10 PM, Christian Reuter <
[email protected]> wrote:
Thank you for your reply Dominik.
I tried to extend the class XSSFWorkbook to be able to call the protected
method
removeRelation of XSSFWorkbook:
public class XSSFWorkbookExt extends XSSFWorkbook {
...
public final boolean removePartRelation(POIXMLDocumentPart part,
boolean removeUnusedParts){
return removeRelation(part, removeUnusedParts);
}
}
then I tried to delete the tables like this:
for (XSSFTable table : sheet.getTables()) {
workbook.removePartRelation(table, true);
}
The code is executed and does the job it is intended to do (I stepped
through with the debugger) but the tables still don't get deleted from the
archive file. When I save the document the tables still exist in the file
as xml files under xl/tables.
No matter if I do the steps above or not: When I open the file from which
I deleted some sheets in Excel and save it again then the tables of the
deleted sheets are gone. I assume that Excel does recognize that all links
to the tables have been removed while POI does not.
Can anyone help on how to improve that?
Thx,
Christian
Am 26.02.2016 um 22:36 schrieb Dominik Stadler:
Hi,
It should be possible to remove these pieces via the low-level POI APIs
for
Relationships/Parts on the XSSFWorkbook before you write out the XSLX,
however I don't know the code well enough to state how exactly you would
go
about that.
Dominik.
On Fri, Feb 26, 2016 at 5:31 PM, Christian Reuter <
[email protected]> wrote:
Hi there,
I am working with XSSFTable and CTTable in my workbooks.
My program opens an .xlsx - template file with POI in order to delete and
recreate some of the sheets in this file and save it as a new file.
The problem is that the deleted sheets as well as the new sheets created
programmatically may contain tables. Unfortunately if there are tables
defined in the deleted sheets they are not deleted when the sheet is
deleted.
I therefore get ID conflicts later if one of my newly created tables has
the same ids as one of the tables which have been left over from the
sheet
deletions.
The only way I see to remove the existing tables is to open the zip file
before reading it as a workbook and deleting the contents of the
subfolder
"xl\tables" which contains the table definitions.
Can somebody help me to find a better solution please?
If not: can someone provide a code snippet for the above mentioned
low-level manipulation of the file?
Thx,
Christian