aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/testcases/org/apache/poi/xssf/usermodel
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2017-06-07 12:39:41 +0000
committerNick Burch <nick@apache.org>2017-06-07 12:39:41 +0000
commit9907b69401ecfa79ebfefeb8aeeda0be6c00e765 (patch)
tree0121013495ed40fba75a7969435c6c9a777ec7bb /src/ooxml/testcases/org/apache/poi/xssf/usermodel
parentbf7d6f41f4d0a5e4ada0e9ef68a17e8f5f6b575f (diff)
downloadpoi-9907b69401ecfa79ebfefeb8aeeda0be6c00e765.tar.gz
poi-9907b69401ecfa79ebfefeb8aeeda0be6c00e765.zip
XSSFTable should format numeric/date cells when used as Column Header names as Excel does, see https://stackoverflow.com/questions/44407111/apache-poi-cant-format-filled-cells-as-numeric
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1797914 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi/xssf/usermodel')
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java78
1 files changed, 78 insertions, 0 deletions
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 69b412a51f..e9e7f3a885 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
@@ -38,6 +38,7 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;
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.CTTableStyleInfo;
public final class TestXSSFTable {
@@ -287,4 +288,81 @@ public final class TestXSSFTable {
table.updateReferences();
assertEquals(11, table.getRowCount());
}
+
+ @Test
+ public void testDifferentHeaderTypes() throws IOException {
+ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TablesWithDifferentHeaders.xlsx");
+ assertEquals(3, wb.getNumberOfSheets());
+ XSSFSheet s;
+ XSSFTable t;
+
+ // TODO Nicer column fetching
+
+ s = wb.getSheet("IntHeaders");
+ assertEquals(1, s.getTables().size());
+ t = s.getTables().get(0);
+ assertEquals("A1:B2", t.getReferences().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("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("Column1", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
+ assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
+ }
+
+ /**
+ * See https://stackoverflow.com/questions/44407111/apache-poi-cant-format-filled-cells-as-numeric
+ */
+ @Test
+ 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
+ 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
+ c1.setCellValue(12);
+ c2.setCellValue(34);
+ c3.setCellValue("AB");
+ c4.setCellValue("CD");
+
+ // Save and re-load
+ wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ s = wb.getSheetAt(0);
+
+ // Check
+ assertEquals(1, s.getTables().size());
+ t = s.getTables().get(0);
+ assertEquals("A1", t.getStartCellReference().formatAsString());
+ assertEquals("B2", t.getEndCellReference().formatAsString());
+
+ // Done
+ wb.close();
+ }
}