summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2017-06-07 13:06:38 +0000
committerNick Burch <nick@apache.org>2017-06-07 13:06:38 +0000
commit9a4caae61e00db6fde9e6008914212c689b0e1d8 (patch)
tree01b07c942aeb1a12970709a92206011c877870b2 /src
parent9907b69401ecfa79ebfefeb8aeeda0be6c00e765 (diff)
downloadpoi-9a4caae61e00db6fde9e6008914212c689b0e1d8.tar.gz
poi-9a4caae61e00db6fde9e6008914212c689b0e1d8.zip
Make it possible to create simple XSSF sheet tables without needing CT classes directly
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1797919 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java49
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java59
2 files changed, 80 insertions, 28 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
index ef8ace666b..7c7a2e6459 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
@@ -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()) {
@@ -229,6 +229,29 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
}
/**
+ * 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
*/
public String getName() {
@@ -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
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
index e9e7f3a885..38585d43b6 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
@@ -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();