]> source.dussan.org Git - poi.git/commitdiff
Make it possible to create simple XSSF sheet tables without needing CT classes directly
authorNick Burch <nick@apache.org>
Wed, 7 Jun 2017 13:06:38 +0000 (13:06 +0000)
committerNick Burch <nick@apache.org>
Wed, 7 Jun 2017 13:06:38 +0000 (13:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1797919 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java

index ef8ace666bc29009135079eee3ece71ed04de80a..7c7a2e645901f15b6354c962914ff80dfd1edab9 100644 (file)
@@ -42,6 +42,7 @@ import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument;
 
 /**
@@ -215,7 +216,6 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
      * @return List of XSSFXmlColumnPr
      */
     public List<XSSFXmlColumnPr> getXmlColumnPrs() {
-        
         if (xmlColumnPr==null) {
             xmlColumnPr = new ArrayList<XSSFXmlColumnPr>();
             for (CTTableColumn column: getTableColumns()) {
@@ -228,6 +228,29 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
         return xmlColumnPr;
     }
     
+    /**
+     * Adds another column to the table.
+     * 
+     * Warning - Return type likely to change!
+     */
+    @Internal("Return type likely to change")
+    public void addColumn() {
+        // Ensure we have Table Columns
+        CTTableColumns columns = ctTable.getTableColumns();
+        if (columns == null) {
+            columns = ctTable.addNewTableColumns();
+        }
+        
+        // Add another Column, and give it a sensible ID
+        CTTableColumn column = columns.addNewTableColumn();
+        int num = columns.sizeOfTableColumnArray();
+        columns.setCount(num);
+        column.setId(num);
+        
+        // Have the Headers updated if possible
+        updateHeaders();
+    }
+    
     /**
      * @return the name of the Table, if set
      */
@@ -321,12 +344,34 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
      * To synchronize with changes to the underlying CTTable,
      * call {@link #updateReferences()}.
      */
-    public AreaReference getReferences() {
+    public AreaReference getCellReferences() {
         return new AreaReference(
                 getStartCellReference(),
                 getEndCellReference()
         );
     }
+    /**
+     * Updates the reference for the cells of the table
+     * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
+     * and synchronizes any changes
+     */
+    public void setCellReferences(AreaReference refs) {
+        // Strip the Sheet name
+        String ref = refs.formatAsString();
+        if (ref.indexOf('!') != -1) {
+            ref = ref.substring(ref.indexOf('!')+1);
+        }
+        
+        // Update
+        ctTable.setRef(ref);
+        if (ctTable.isSetAutoFilter()) {
+            ctTable.getAutoFilter().setRef(ref);
+        }
+        
+        // Have everything recomputed
+        updateReferences();
+        updateHeaders();
+    }
     
     /**
      * @return The reference for the cell in the top-left part of the table
index e9e7f3a885b89fe1f85dd2ec6bff2e51e7a270df..38585d43b6abefe8bd6b021f55a796e0d2c22ca5 100644 (file)
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.util.AreaReference;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -301,21 +302,21 @@ public final class TestXSSFTable {
         s = wb.getSheet("IntHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B2", t.getReferences().formatAsString());
+        assertEquals("A1:B2", t.getCellReferences().formatAsString());
         assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("34", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
         
         s = wb.getSheet("FloatHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B2", t.getReferences().formatAsString());
+        assertEquals("A1:B2", t.getCellReferences().formatAsString());
         assertEquals("12.34", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
         
         s = wb.getSheet("NoExplicitHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B3", t.getReferences().formatAsString());
+        assertEquals("A1:B3", t.getCellReferences().formatAsString());
         assertEquals("Column1", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
     }
@@ -327,30 +328,31 @@ public final class TestXSSFTable {
     public void testNumericCellsInTable() throws IOException {
         XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet s = wb.createSheet();
-
-        //Setting up the CTTable
-        XSSFTable t = s.createTable();
-        CTTable ctt = t.getCTTable();
-        ctt.setId(1);
-        ctt.setName("CT Table Test");
-        ctt.setRef("A1:B2");
-        CTTableColumns cttcs = ctt.addNewTableColumns();
-        CTTableColumn cttc1 = cttcs.addNewTableColumn();
-        cttc1.setId(1);
-        CTTableColumn cttc2 = cttcs.addNewTableColumn();
-        cttc2.setId(2);
-
-        //Creating the cells
+        
+        // Create some cells, some numeric, some not
         Cell c1 = s.createRow(0).createCell(0);
-        XSSFCell c2 = s.getRow(0).createCell(1);
-        XSSFCell c3 = s.createRow(1).createCell(0);
-        XSSFCell c4 = s.getRow(1).createCell(1);
-
-        // Inserting values; some numeric strings, some alphabetical strings
+        Cell c2 = s.getRow(0).createCell(1);
+        Cell c3 = s.getRow(0).createCell(2);
+        Cell c4 = s.createRow(1).createCell(0);
+        Cell c5 = s.getRow(1).createCell(1);
+        Cell c6 = s.getRow(1).createCell(2);
         c1.setCellValue(12);
-        c2.setCellValue(34); 
-        c3.setCellValue("AB");
-        c4.setCellValue("CD");
+        c2.setCellValue(34.56);
+        c3.setCellValue("ABCD");
+        c4.setCellValue("AB");
+        c5.setCellValue("CD");
+        c6.setCellValue("EF");
+
+        // Setting up the CTTable
+        XSSFTable t = s.createTable();
+        t.setName("TableTest");
+        t.setDisplayName("CT Table Test");
+        t.addColumn();
+        t.addColumn();
+        t.addColumn();
+        t.setCellReferences(new AreaReference(
+                new CellReference(c1), new CellReference(c6)
+        ));
 
         // Save and re-load
         wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
@@ -360,7 +362,12 @@ public final class TestXSSFTable {
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
         assertEquals("A1", t.getStartCellReference().formatAsString());
-        assertEquals("B2", t.getEndCellReference().formatAsString());
+        assertEquals("C2", t.getEndCellReference().formatAsString());
+        
+        // TODO Nicer column fetching
+        assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
+        assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
+        assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
         
         // Done
         wb.close();