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
This commit is contained in:
Nick Burch 2017-06-07 13:06:38 +00:00
parent 2ce00a5a78
commit fd0e150a94
2 changed files with 80 additions and 28 deletions

View 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

View 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();