Deleting a sheet did not delete table parts and relations. Deleting a table needs to also delete any queryTable relations and parts.
Previous behavior didn't result in documents Excel complained about, but left dead entries in the ZIP structure, which made it bigger and bugged me. This change does not attempt to delete query connection definitions, as those aren't referenced as relations, and don't have a usage counter to ensure we only delete them if there are no other references. In my samples I had query tables on multiple sheets using the same connection definition, and wanted to delete only one sheet/table but leave others. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1803317 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9f28e36438
commit
f9b9c0d18b
@ -3957,6 +3957,24 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
||||
return new ArrayList<XSSFTable>(tables.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove table references and relations
|
||||
* @param t table to remove
|
||||
*/
|
||||
public void removeTable(XSSFTable t) {
|
||||
long id = t.getCTTable().getId();
|
||||
Map.Entry<String, XSSFTable> toDelete = null;
|
||||
|
||||
for (Map.Entry<String, XSSFTable> entry : tables.entrySet()) {
|
||||
if (entry.getValue().getCTTable().getId() == id) toDelete = entry;
|
||||
}
|
||||
if (toDelete != null) {
|
||||
removeRelation(getRelationById(toDelete.getKey()), true);
|
||||
tables.remove(toDelete.getKey());
|
||||
toDelete.getValue().onTableDelete();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public XSSFSheetConditionalFormatting getSheetConditionalFormatting(){
|
||||
return new XSSFSheetConditionalFormatting(this);
|
||||
@ -4396,6 +4414,20 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
||||
XSSFIgnoredErrorHelper.addIgnoredErrors(ctIgnoredError, ref, ignoredErrorTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* called when a sheet is being deleted/removed from a workbook, to clean up relations and other document pieces tied to the sheet
|
||||
*/
|
||||
protected void onSheetDelete() {
|
||||
for (RelationPart part : getRelationParts()) {
|
||||
if (part.getDocumentPart() instanceof XSSFTable) {
|
||||
// call table delete
|
||||
removeTable((XSSFTable) part.getDocumentPart());
|
||||
continue;
|
||||
}
|
||||
removeRelation(part.getDocumentPart(), true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the OleObject which links shapes with embedded resources
|
||||
*
|
||||
|
@ -29,6 +29,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.poi.POIXMLDocumentPart;
|
||||
import org.apache.poi.POIXMLDocumentPart.RelationPart;
|
||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
||||
import org.apache.poi.ss.SpreadsheetVersion;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
@ -623,4 +624,14 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove relations
|
||||
*/
|
||||
protected void onTableDelete() {
|
||||
for (RelationPart part : getRelationParts()) {
|
||||
removeRelation(part.getDocumentPart(), true);
|
||||
}
|
||||
}
|
||||
}
|
@ -1366,6 +1366,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
* @param index the 0-based index of the sheet to delete
|
||||
*/
|
||||
private void onSheetDelete(int index) {
|
||||
// remove all sheet relations
|
||||
final XSSFSheet sheet = getSheetAt(index);
|
||||
|
||||
sheet.onSheetDelete();
|
||||
|
||||
//delete the CTSheet reference from workbook.xml
|
||||
workbook.getSheets().removeSheet(index);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user