From e9b19a79f7b48df9661b3b307bfbe97f75bf044e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 8 Apr 2011 15:07:35 +0000 Subject: [PATCH] Add support for adding a table to a XSSFSheet git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1090289 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../java/org/apache/poi/xssf/model/Table.java | 18 ++++++++++ .../apache/poi/xssf/usermodel/XSSFSheet.java | 36 ++++++++++++++----- .../poi/xssf/usermodel/TestXSSFBugs.java | 32 +++++++++++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 729ba3e897..6651b10f2d 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Support for adding a table to a XSSFSheet Improve HSMF MAPIMessage access to the HTML and RTF versions of the message body (where available) Add new method to HSMF of MAPIMessage.has7BitEncodingStrings() to make it easier to decide when encoding guessing is needed OutlookTextExtractor now requests 7 bit encoding guessing diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Table.java b/src/ooxml/java/org/apache/poi/xssf/model/Table.java index 412a425cac..ee887214f4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/Table.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/Table.java @@ -192,6 +192,17 @@ public class Table extends POIXMLDocumentPart { public String getName() { return ctTable.getName(); } + + /** + * Changes the name of the Table + */ + public void setName(String name) { + if(name == null) { + ctTable.unsetName(); + return; + } + ctTable.setName(name); + } /** * @return the display name of the Table, if set @@ -200,6 +211,13 @@ public class Table extends POIXMLDocumentPart { return ctTable.getDisplayName(); } + /** + * Changes the display name of the Table + */ + public void setDisplayName(String name) { + ctTable.setDisplayName(name); + } + /** * @return the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4) */ 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 52dc4afeeb..80cfd91476 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -88,6 +88,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * Master shared formula is the first formula in a group of shared formulas is saved in the f element. */ private Map sharedFormulas; + private TreeMap tables; private List arrayFormulas; private XSSFDataValidationHelper dataValidationHelper; @@ -151,6 +152,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { sheetComments = (CommentsTable)p; break; } + if(p instanceof Table) { + tables.put( p.getPackageRelationship().getId(), (Table)p ); + } } // Process external hyperlinks for the sheet, if there are any @@ -171,6 +175,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support private void initRows(CTWorksheet worksheet) { _rows = new TreeMap(); + tables = new TreeMap(); sharedFormulas = new HashMap(); arrayFormulas = new ArrayList(); for (CTRow row : worksheet.getSheetData().getRowArray()) { @@ -2956,17 +2961,32 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { return new XSSFAutoFilter(this); } + /** + * Creates a new Table, and associates it with this Sheet + */ + public Table createTable() { + if(! worksheet.isSetTableParts()) { + worksheet.addNewTableParts(); + } + + CTTableParts tblParts = worksheet.getTableParts(); + CTTablePart tbl = tblParts.addNewTablePart(); + + Table table = (Table)createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tblParts.sizeOfTablePartArray()); + tbl.setId(table.getPackageRelationship().getId()); + + tables.put(tbl.getId(), table); + + return table; + } + /** * Returns any tables associated with this Sheet */ public List getTables() { - List
tables = new ArrayList
(); - for(POIXMLDocumentPart p : getRelations()) { - if (p.getPackageRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation())) { - Table table = (Table) p; - tables.add(table); - } - } - return tables; + List
tableList = new ArrayList
( + tables.values() + ); + return tableList; } } 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 9928949a27..cf873f4d88 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -892,6 +892,38 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("Tabella1", t.getName()); assertEquals("Tabella1", t.getDisplayName()); assertEquals("A1:C3", t.getCTTable().getRef()); + + + // Add some more tables, and check + t = s2.createTable(); + t.setName("New 2"); + t.setDisplayName("New 2"); + t = s3.createTable(); + t.setName("New 3"); + t.setDisplayName("New 3"); + + wb = XSSFTestDataSamples.writeOutAndReadBack(wb); + s1 = wb.getSheetAt(0); + s2 = wb.getSheetAt(1); + s3 = wb.getSheetAt(2); + s4 = wb.getSheetAt(3); + assertEquals(0, s1.getTables().size()); + assertEquals(2, s2.getTables().size()); + assertEquals(1, s3.getTables().size()); + assertEquals(0, s4.getTables().size()); + + t = s2.getTables().get(0); + assertEquals("Tabella1", t.getName()); + assertEquals("Tabella1", t.getDisplayName()); + assertEquals("A1:C3", t.getCTTable().getRef()); + + t = s2.getTables().get(1); + assertEquals("New 2", t.getName()); + assertEquals("New 2", t.getDisplayName()); + + t = s3.getTables().get(0); + assertEquals("New 3", t.getName()); + assertEquals("New 3", t.getDisplayName()); } /** -- 2.39.5