[bug-66212] try to remove table part for table when removing table

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903395 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-08-13 16:11:03 +00:00
parent 839594b73e
commit 62d8b8df57
2 changed files with 22 additions and 4 deletions

View File

@ -4312,10 +4312,16 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx
removeRelation(getRelationById(toDelete.getKey()), true);
tables.remove(toDelete.getKey());
toDelete.getValue().onTableDelete();
OPCPackage opcPackage = getWorkbook().getPackage();
PackagePart packagePart = t.getPackagePart();
if (packagePart != null && opcPackage.containPart(packagePart.getPartName())) {
opcPackage.removePart(packagePart);
CTTableParts tblParts = worksheet.getTableParts();
int matchedPos = -1;
for (int i = 0; i < tblParts.sizeOfTablePartArray(); i++) {
if (toDelete.getKey().equals(tblParts.getTablePartArray(i).getId())) {
matchedPos = i;
break;
}
}
if (matchedPos != -1) {
tblParts.removeTablePart(matchedPos);
}
}
}

View File

@ -45,6 +45,7 @@ import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public final class TestXSSFTable {
@ -726,4 +727,15 @@ public final class TestXSSFTable {
assertEquals(cols.get(4).getName(), updatedCols.get(4).getName());
}
}
@Test
void bug66212() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("table-sample.xlsx")) {
XSSFTable table = wb.getTable("Tabelle1");
XSSFSheet sheet = table.getXSSFSheet();
assertEquals(1, sheet.getCTWorksheet().getTableParts().sizeOfTablePartArray());
sheet.removeTable(table);
assertEquals(0, sheet.getCTWorksheet().getTableParts().sizeOfTablePartArray());
}
}
}