From 214695e09c9971cba7161045e99d4055bef878c1 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 1 Jan 2018 14:39:33 +0000 Subject: [PATCH] Bug 61543: do not fail with "part already exists" when tables are created/removed git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819773 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFSheet.java | 44 +++++++++++++------ .../poi/xssf/usermodel/TestXSSFBugs.java | 21 +++++++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 75eb69b76..259c31445 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -3957,24 +3957,40 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * Creates a new Table, and associates it with this Sheet */ public XSSFTable createTable() { - if(! worksheet.isSetTableParts()) { - worksheet.addNewTableParts(); - } + if(! worksheet.isSetTableParts()) { + worksheet.addNewTableParts(); + } - CTTableParts tblParts = worksheet.getTableParts(); - CTTablePart tbl = tblParts.addNewTablePart(); + CTTableParts tblParts = worksheet.getTableParts(); + CTTablePart tbl = tblParts.addNewTablePart(); - // Table numbers need to be unique in the file, not just - // unique within the sheet. Find the next one - int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1; - RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false); - XSSFTable table = rp.getDocumentPart(); - tbl.setId(rp.getRelationship().getId()); - table.getCTTable().setId(tableNumber); + // Table numbers need to be unique in the file, not just + // unique within the sheet. Find the next one + int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1; - tables.put(tbl.getId(), table); + // the id could already be taken after insertion/deletion of different tables + outerloop: + while(true) { + for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) { + String fileName = XSSFRelation.TABLE.getFileName(tableNumber); + if(fileName.equals(packagePart.getPartName().getName())) { + // duplicate found, increase the number and start iterating again + tableNumber++; + continue outerloop; + } + } - return table; + break; + } + + RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false); + XSSFTable table = rp.getDocumentPart(); + tbl.setId(rp.getRelationship().getId()); + table.getCTTable().setId(tableNumber); + + tables.put(tbl.getId(), table); + + return table; } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index f5a51c100..131b441d6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -3231,4 +3231,25 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("AND($A1>=EDATE($D$6,3),$B1>0)", rules.get(0).getFormula1()); } } + + @Test + public void test61543() throws IOException { + XSSFWorkbook wb = new XSSFWorkbook(); + + XSSFSheet sheet = wb.createSheet(); + XSSFTable table1 = sheet.createTable(); + XSSFTable table2 = sheet.createTable(); + XSSFTable table3 = sheet.createTable(); + + sheet.removeTable(table1); + + sheet.createTable(); + + sheet.removeTable(table2); + sheet.removeTable(table3); + + sheet.createTable(); + + wb.close(); + } }