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
This commit is contained in:
Dominik Stadler 2018-01-01 14:39:33 +00:00
parent 7e04bd33e5
commit 5923cb895e
2 changed files with 51 additions and 14 deletions

View File

@ -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;
}
/**

View File

@ -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();
}
}