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:
parent
e05a94e8ee
commit
214695e09c
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user