diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index e9fd943ba8..055f8452d8 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -392,13 +392,17 @@ public interface Sheet extends Iterable { PrintSetup getPrintSetup(); /** - * Gets the user model for the document header. + * Gets the user model for the default document header. + * Note that XSSF offers more kinds of document + * headers than HSSF does * @return The Document header. */ Header getHeader(); /** - * Gets the user model for the document footer. + * Gets the user model for the default document footer. + * Note that XSSF offers more kinds of document + * footers than HSSF does. * @return The Document footer. */ Footer getFooter(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java index a215285a21..759b3e4d26 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java @@ -32,7 +32,11 @@ public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{ } public void setText(String text) { - getHeaderFooter().setEvenFooter(text); + if(text == null) { + getHeaderFooter().unsetEvenFooter(); + } else { + getHeaderFooter().setEvenFooter(text); + } } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java index 670b498012..0a63110ba0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java @@ -32,7 +32,11 @@ public class XSSFEvenHeader extends XSSFHeaderFooter implements Header{ } public void setText(String text) { - getHeaderFooter().setEvenHeader(text); + if(text == null) { + getHeaderFooter().unsetEvenHeader(); + } else { + getHeaderFooter().setEvenHeader(text); + } } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java index e9a1f37764..7d2df77d75 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java @@ -32,7 +32,10 @@ public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{ } public void setText(String text) { - getHeaderFooter().setFirstFooter(text); + if(text == null) { + getHeaderFooter().unsetFirstFooter(); + } else { + getHeaderFooter().setFirstFooter(text); + } } - } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java index 17e5a70861..27a7520d42 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java @@ -32,7 +32,11 @@ public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{ } public void setText(String text) { - getHeaderFooter().setFirstHeader(text); + if(text == null) { + getHeaderFooter().unsetFirstHeader(); + } else { + getHeaderFooter().setFirstHeader(text); + } } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java index 709212ef8d..8e3143b0d1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java @@ -32,7 +32,10 @@ public class XSSFOddFooter extends XSSFHeaderFooter implements Footer{ } public void setText(String text) { - getHeaderFooter().setOddFooter(text); + if(text == null) { + getHeaderFooter().unsetOddFooter(); + } else { + getHeaderFooter().setOddFooter(text); + } } - } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java index e03a6491f0..5ee37cc00a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java @@ -32,7 +32,10 @@ public class XSSFOddHeader extends XSSFHeaderFooter implements Header{ } public void setText(String text) { - getHeaderFooter().setOddHeader(text); + if(text == null) { + getHeaderFooter().unsetOddHeader(); + } else { + getHeaderFooter().setOddHeader(text); + } } - } 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 18c0e8f161..a76711a7f3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -381,14 +381,7 @@ public class XSSFSheet implements Sheet { return getSheetTypePageSetUpPr().getFitToPage(); } - public Footer getFooter() { - return getOddFooter(); - } - public Footer getOddFooter() { - return new XSSFOddFooter(getSheetTypeHeaderFooter()); - } - protected CTHeaderFooter getSheetTypeHeaderFooter() { if (worksheet.getHeaderFooter() == null) { worksheet.setHeaderFooter(CTHeaderFooter.Factory.newInstance()); @@ -396,31 +389,80 @@ public class XSSFSheet implements Sheet { return worksheet.getHeaderFooter(); } + /** + * Returns the default footer for the sheet, + * creating one as needed. + * You may also want to look at + * {@link #getFirstFooter()}, + * {@link #getOddFooter()} and + * {@link #getEvenFooter()} + */ + public Footer getFooter() { + // The default footer is an odd footer + return getOddFooter(); + } + + /** + * Returns the default header for the sheet, + * creating one as needed. + * You may also want to look at + * {@link #getFirstHeader()}, + * {@link #getOddHeader()} and + * {@link #getEvenHeader()} + */ + public Header getHeader() { + // The default header is an odd header + return getOddHeader(); + } + + /** + * Returns the odd footer. Used on all pages unless + * other footers also present, when used on only + * odd pages. + */ + public Footer getOddFooter() { + return new XSSFOddFooter(getSheetTypeHeaderFooter()); + } + /** + * Returns the even footer. Not there by default, but + * when set, used on even pages. + */ public Footer getEvenFooter() { return new XSSFEvenFooter(getSheetTypeHeaderFooter()); } - + /** + * Returns the first page footer. Not there by + * default, but when set, used on the first page. + */ public Footer getFirstFooter() { return new XSSFFirstFooter(getSheetTypeHeaderFooter()); } - public Header getHeader() { - return getOddHeader(); - } - + /** + * Returns the odd header. Used on all pages unless + * other headers also present, when used on only + * odd pages. + */ public Header getOddHeader() { return new XSSFOddHeader(getSheetTypeHeaderFooter()); } - + /** + * Returns the even header. Not there by default, but + * when set, used on even pages. + */ public Header getEvenHeader() { return new XSSFEvenHeader(getSheetTypeHeaderFooter() ); } - + /** + * Returns the first page header. Not there by + * default, but when set, used on the first page. + */ public Header getFirstHeader() { return new XSSFFirstHeader(getSheetTypeHeaderFooter()); } + public boolean getHorizontallyCenter() { return getSheetTypePrintOptions().getHorizontalCentered(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index c709d81be3..ad8dfc9aee 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -281,10 +281,14 @@ public class TestXSSFSheet extends TestCase { public void testGetFooter() { XSSFWorkbook workbook = new XSSFWorkbook(); - Sheet sheet = workbook.createSheet("Sheet 1"); + XSSFSheet sheet = (XSSFSheet)workbook.createSheet("Sheet 1"); assertNotNull(sheet.getFooter()); sheet.getFooter().setCenter("test center footer"); assertEquals("test center footer", sheet.getFooter().getCenter()); + + // Default is odd footer + assertNotNull(sheet.getOddFooter()); + assertEquals("test center footer", sheet.getOddFooter().getCenter()); } public void testGetAllHeadersFooters() { @@ -321,6 +325,9 @@ public class TestXSSFSheet extends TestCase { sheet.getOddHeader().setCenter("odd header center"); assertEquals("odd header center", sheet.getOddHeader().getCenter()); + // Defaults are odd + assertEquals("odd footer left", sheet.getFooter().getLeft()); + assertEquals("odd header center", sheet.getHeader().getCenter()); } public void testGetSetColumnWidth() {