Add support for adding a table to a XSSFSheet

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1090289 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-04-08 15:07:35 +00:00
parent 11cd9209d8
commit e9b19a79f7
4 changed files with 79 additions and 8 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta3" date="2011-??-??">
<action dev="poi-developers" type="add">Support for adding a table to a XSSFSheet</action>
<action dev="poi-developers" type="add">Improve HSMF MAPIMessage access to the HTML and RTF versions of the message body (where available)</action>
<action dev="poi-developers" type="add">Add new method to HSMF of MAPIMessage.has7BitEncodingStrings() to make it easier to decide when encoding guessing is needed</action>
<action dev="poi-developers" type="fix">OutlookTextExtractor now requests 7 bit encoding guessing</action>

View File

@ -193,6 +193,17 @@ public class Table extends POIXMLDocumentPart {
return ctTable.getName();
}
/**
* Changes the name of the Table
*/
public void setName(String name) {
if(name == null) {
ctTable.unsetName();
return;
}
ctTable.setName(name);
}
/**
* @return the display name of the Table, if set
*/
@ -200,6 +211,13 @@ public class Table extends POIXMLDocumentPart {
return ctTable.getDisplayName();
}
/**
* Changes the display name of the Table
*/
public void setDisplayName(String name) {
ctTable.setDisplayName(name);
}
/**
* @return the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4)
*/

View File

@ -88,6 +88,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* Master shared formula is the first formula in a group of shared formulas is saved in the f element.
*/
private Map<Integer, CTCellFormula> sharedFormulas;
private TreeMap<String,Table> tables;
private List<CellRangeAddress> arrayFormulas;
private XSSFDataValidationHelper dataValidationHelper;
@ -151,6 +152,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
sheetComments = (CommentsTable)p;
break;
}
if(p instanceof Table) {
tables.put( p.getPackageRelationship().getId(), (Table)p );
}
}
// Process external hyperlinks for the sheet, if there are any
@ -171,6 +175,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
private void initRows(CTWorksheet worksheet) {
_rows = new TreeMap<Integer, XSSFRow>();
tables = new TreeMap<String, Table>();
sharedFormulas = new HashMap<Integer, CTCellFormula>();
arrayFormulas = new ArrayList<CellRangeAddress>();
for (CTRow row : worksheet.getSheetData().getRowArray()) {
@ -2956,17 +2961,32 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return new XSSFAutoFilter(this);
}
/**
* Creates a new Table, and associates it with this Sheet
*/
public Table createTable() {
if(! worksheet.isSetTableParts()) {
worksheet.addNewTableParts();
}
CTTableParts tblParts = worksheet.getTableParts();
CTTablePart tbl = tblParts.addNewTablePart();
Table table = (Table)createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tblParts.sizeOfTablePartArray());
tbl.setId(table.getPackageRelationship().getId());
tables.put(tbl.getId(), table);
return table;
}
/**
* Returns any tables associated with this Sheet
*/
public List<Table> getTables() {
List<Table> tables = new ArrayList<Table>();
for(POIXMLDocumentPart p : getRelations()) {
if (p.getPackageRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation())) {
Table table = (Table) p;
tables.add(table);
}
}
return tables;
List<Table> tableList = new ArrayList<Table>(
tables.values()
);
return tableList;
}
}

View File

@ -892,6 +892,38 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
assertEquals("Tabella1", t.getName());
assertEquals("Tabella1", t.getDisplayName());
assertEquals("A1:C3", t.getCTTable().getRef());
// Add some more tables, and check
t = s2.createTable();
t.setName("New 2");
t.setDisplayName("New 2");
t = s3.createTable();
t.setName("New 3");
t.setDisplayName("New 3");
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
s1 = wb.getSheetAt(0);
s2 = wb.getSheetAt(1);
s3 = wb.getSheetAt(2);
s4 = wb.getSheetAt(3);
assertEquals(0, s1.getTables().size());
assertEquals(2, s2.getTables().size());
assertEquals(1, s3.getTables().size());
assertEquals(0, s4.getTables().size());
t = s2.getTables().get(0);
assertEquals("Tabella1", t.getName());
assertEquals("Tabella1", t.getDisplayName());
assertEquals("A1:C3", t.getCTTable().getRef());
t = s2.getTables().get(1);
assertEquals("New 2", t.getName());
assertEquals("New 2", t.getDisplayName());
t = s3.getTables().get(0);
assertEquals("New 3", t.getName());
assertEquals("New 3", t.getDisplayName());
}
/**