]> source.dussan.org Git - poi.git/commitdiff
[bug-66212] try to remove table part for table when removing table
authorPJ Fanning <fanningpj@apache.org>
Sat, 13 Aug 2022 16:11:03 +0000 (16:11 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 13 Aug 2022 16:11:03 +0000 (16:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903395 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java

index 1459bc872d1c79efcb2fa7da9d875c42f6367428..3e19fe24160bce647a717bf030b359640eb6c3d9 100644 (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);
             }
         }
     }
index 20aa24bb93eac381f255989c024f5dc8074cc4d1..254f47639952bfa44e8625f0d408cfafd6df0d96 100644 (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());
+        }
+    }
 }