/** * Creates a new Table, and associates it with this Sheet. *

* The table is assigned a default display name (since 4.1.1) which can be overridden * by calling {@code setDisplayName}. The default display name is guaranteed to not conflict * with the names of any {@code XSSFName} or {@code XSSFTable} in the workbook. * * @param tableArea * the area that the table should cover, should not be null * @return the created table * @since 4.0.0 */ public XSSFTable createTable(AreaReference tableArea) { if (!worksheet.isSetTableParts()) { worksheet.addNewTableParts(); } 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; // the id could already be taken after insertion/deletion of different tables boolean loop = true; while(loop) { loop = false; 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++; loop = true; } } } 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); if(tableArea != null) { table.setArea(tableArea); } // Bug fix 62906 // Set the default name of the table. This must not conflict with any defined names. final boolean bUseDMGFix=true; if(bUseDMGFix) { final XSSFWorkbook workbook=getWorkbook(); while(tableNumber