]> source.dussan.org Git - poi.git/commitdiff
Fix bug #50829 - Support for getting the tables associated with a XSSFSheet
authorNick Burch <nick@apache.org>
Fri, 25 Feb 2011 21:44:09 +0000 (21:44 +0000)
committerNick Burch <nick@apache.org>
Fri, 25 Feb 2011 21:44:09 +0000 (21:44 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1074710 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/model/Table.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
test-data/spreadsheet/WithTable.xlsx [new file with mode: 0644]

index bf64050a3a923dab0d974272a827ac0644fad6bb..85e9519a1b7648f0a6861e1c1a88eba047b67fcd 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta1" date="2010-??-??">
+           <action dev="poi-developers" type="add">50829 - Support for getting the tables associated with a XSSFSheet</action>
            <action dev="poi-developers" type="fix">50299 - More XSSFColor updates for ARGB vs RGB</action>
            <action dev="poi-developers" type="fix">50581 - Use stax:stax-api instead of org.apache.geronimo.specs:geronimo-stax-api_1.0_spec</action>
            <action dev="poi-developers" type="fix">50786 - Fix XSSFColor to fetch the RGB values of old-style indexed colours</action>
index f69c85c46c0cda50b8c54f900f98ced9fa7caaa1..412a425cacba57efa54799e70852f633e370853a 100644 (file)
@@ -185,6 +185,20 @@ public class Table extends POIXMLDocumentPart {
                }
                return xmlColumnPr;
        }
+       
+       /**
+        * @return the name of the Table, if set
+        */
+       public String getName() {
+          return ctTable.getName();
+       }
+
+   /**
+    * @return the display name of the Table, if set
+    */
+   public String getDisplayName() {
+      return ctTable.getDisplayName();
+   }
 
        /**
         * @return  the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4)
index 0fcd1d1103936fd9b3d07274582c18ebe92249a8..73628516a6aefada37899ab9567044d37d17c3fa 100644 (file)
@@ -55,6 +55,7 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
 import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
 import org.apache.xmlbeans.XmlException;
@@ -2954,4 +2955,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
         return new XSSFAutoFilter(this);
     }
+    
+    /**
+     * Returns any tables associated with this Sheet
+     */
+    public List<Table> getTables() {
+       List<Table> tables = new ArrayList<Table>();
+       for(POIXMLDocumentPart p : getRelations()) {
+          if (p.getPackageRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation())) {
+             Table table = (Table) p;
+             tables.add(table);
+          }
+       }
+       return tables;
+    }
 }
index 56a7004d4a25d96afb35ac8ddbe8ce2725656917..410ab61eb91e26a7acbc005d3ab6b05d22f80b78 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.util.List;
+
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.XSSFITestDataProvider;
@@ -24,6 +26,7 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.model.CalculationChain;
+import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
 import org.apache.poi.util.HexDump;
 import org.apache.poi.hssf.record.PasswordRecord;
@@ -1035,4 +1038,30 @@ public final class TestXSSFSheet extends BaseTestSheet {
 
     }
 
+    /**
+     * See bug #50829
+     */
+    public void testTables() {
+       XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTable.xlsx");
+       assertEquals(3, wb.getNumberOfSheets());
+       
+       // Check the table sheet
+       XSSFSheet s1 = wb.getSheetAt(0);
+       assertEquals("a", s1.getRow(0).getCell(0).getRichStringCellValue().toString());
+       assertEquals(1.0, s1.getRow(1).getCell(0).getNumericCellValue());
+       
+       List<Table> tables = s1.getTables();
+       assertNotNull(tables);
+       assertEquals(1, tables.size());
+       
+       Table table = tables.get(0);
+       assertEquals("Tabella1", table.getName());
+       assertEquals("Tabella1", table.getDisplayName());
+       
+       // And the others
+       XSSFSheet s2 = wb.getSheetAt(1);
+       assertEquals(0, s2.getTables().size());
+       XSSFSheet s3 = wb.getSheetAt(2);
+       assertEquals(0, s3.getTables().size());
+    }
 }
diff --git a/test-data/spreadsheet/WithTable.xlsx b/test-data/spreadsheet/WithTable.xlsx
new file mode 100644 (file)
index 0000000..5177e0a
Binary files /dev/null and b/test-data/spreadsheet/WithTable.xlsx differ