summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2017-06-07 13:35:20 +0000
committerNick Burch <nick@apache.org>2017-06-07 13:35:20 +0000
commite9a9259a301fd54d49ec9dbc72bed44fb35ad200 (patch)
tree90e5f9ef94b655fbc05d2a9a78474550ad8d341b
parentc8973d9ed54313f89c86f952c4fbf0684f66b74f (diff)
downloadpoi-e9a9259a301fd54d49ec9dbc72bed44fb35ad200.tar.gz
poi-e9a9259a301fd54d49ec9dbc72bed44fb35ad200.zip
Update the CreateTable example to reduce the use of the raw CT classes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1797923 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java57
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java1
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java4
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java2
4 files changed, 35 insertions, 29 deletions
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java
index 056c67fc3f..a2cc063568 100644
--- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java
+++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java
@@ -26,11 +26,8 @@ import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
+import org.apache.poi.xssf.usermodel.XSSFTableStyleInfo;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-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;
/**
* Demonstrates how to create a simple table using Apache POI.
@@ -42,48 +39,52 @@ public class CreateTable {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
- //Create
+ // Create
XSSFTable table = sheet.createTable();
- table.setDisplayName("Test");
- CTTable cttable = table.getCTTable();
+ table.setName("Test");
+ table.setDisplayName("Test_Table");
- //Style configurations
- CTTableStyleInfo style = cttable.addNewTableStyleInfo();
+ // For now, create the initial style in a low-level way
+ table.getCTTable().addNewTableStyleInfo();
+ table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2");
+
+ // Style the table
+ XSSFTableStyleInfo style = (XSSFTableStyleInfo)table.getStyle();
style.setName("TableStyleMedium2");
style.setShowColumnStripes(false);
style.setShowRowStripes(true);
+ style.setFirstColumn(false);
+ style.setLastColumn(false);
+ style.setShowRowStripes(true);
+ style.setShowColumnStripes(true);
- //Set which area the table should be placed in
- AreaReference reference = new AreaReference(new CellReference(0, 0),
- new CellReference(2,2));
- cttable.setRef(reference.formatAsString());
- cttable.setId(1);
- cttable.setName("Test");
- cttable.setTotalsRowCount(1);
-
- CTTableColumns columns = cttable.addNewTableColumns();
- columns.setCount(3);
- CTTableColumn column;
+ // Set the values for the table
XSSFRow row;
XSSFCell cell;
for(int i=0; i<3; i++) {
- //Create column
- column = columns.addNewTableColumn();
- column.setName("Column");
- column.setId(i+1);
- //Create row
+ // Create row
row = sheet.createRow(i);
for(int j=0; j<3; j++) {
- //Create cell
+ // Create cell
cell = row.createCell(j);
if(i == 0) {
- cell.setCellValue("Column"+j);
+ cell.setCellValue("Column"+(j+1));
} else {
- cell.setCellValue("0");
+ cell.setCellValue((i+1)*(j+1));
}
}
}
+ // Create the columns
+ table.addColumn();
+ table.addColumn();
+ table.addColumn();
+ // Set which area the table should be placed in
+ AreaReference reference = new AreaReference(new CellReference(0, 0),
+ new CellReference(2,2));
+ table.setCellReferences(reference);
+
+ // Save
FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
wb.write(fileOut);
fileOut.close();
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 8d62661e68..a67a16ad19 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
@@ -3975,6 +3975,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false);
XSSFTable table = rp.getDocumentPart();
tbl.setId(rp.getRelationship().getId());
+ table.getCTTable().setId(tableNumber);
tables.put(tbl.getId(), table);
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 7c7a2e6459..16fae4b437 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
@@ -343,6 +343,8 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
* Does not track updates to underlying changes to CTTable
* To synchronize with changes to the underlying CTTable,
* call {@link #updateReferences()}.
+ *
+ * @since 3.17 beta 1
*/
public AreaReference getCellReferences() {
return new AreaReference(
@@ -354,6 +356,8 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
* 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
+ *
+ * @since 3.17 beta 1
*/
public void setCellReferences(AreaReference refs) {
// Strip the Sheet name
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 38585d43b6..bbf49bc011 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
@@ -346,7 +346,7 @@ public final class TestXSSFTable {
// Setting up the CTTable
XSSFTable t = s.createTable();
t.setName("TableTest");
- t.setDisplayName("CT Table Test");
+ t.setDisplayName("CT_Table_Test");
t.addColumn();
t.addColumn();
t.addColumn();