Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java (working copy) @@ -21,16 +21,38 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + * + * First page footer content. Corresponds to first printed page. + * The first logical page in the sheet may not be printed, for example, if the print area is specified to + * be a range such that it falls outside the first page's scope. + * + */ public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{ + /** + * Create an instance of XSSFFirstFooter from the supplied XML bean + * @see XSSFSheet#getFirstFooter() + * @param headerFooter + */ protected XSSFFirstFooter(CTHeaderFooter headerFooter) { super(headerFooter); + headerFooter.setDifferentFirst(true); } + /** + * Get the content text representing the footer + * @return text + */ public String getText() { return getHeaderFooter().getFirstFooter(); } + /** + * Set a text for the footer. If null unset the value. + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the footer. + */ public void setText(String text) { if(text == null) { getHeaderFooter().unsetFirstFooter(); Index: src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java (working copy) @@ -18,6 +18,7 @@ package org.apache.poi.xssf.usermodel.extensions; import org.apache.poi.ss.usermodel.HeaderFooter; +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; @@ -26,24 +27,94 @@ * * For a list of all the different fields that can be * placed into a header or footer, such as page number, - * bold, underline etc, see - * {@link org.apache.poi.hssf.usermodel.HeaderFooter}. + * bold, underline etc, see the follow formatting syntax + * + * Header/Footer Formatting Syntax + *

+ * There are a number of formatting codes that can be written inline with the actual header / footer text, which + * affect the formatting in the header or footer. + *

+ * + * This example shows the text "Center Bold Header" on the first line (center section), and the date on the second + * line (center section). + * &CCenter &"-,Bold"Bold &"-,Regular"Header_x000A_&D + * + * General Rules: + * There is no required order in which these codes must appear. + * The first occurrence of the following codes turns the formatting ON, the second occurrence turns it OFF again: + * + *
+ *
&L
code for "left section" (there are three header / footer locations, "left", "center", and "right"). When + * two or more occurrences of this section marker exist, the contents from all markers are concatenated, in the + * order of appearance, and placed into the left section.
+ *
&P
code for "current page #"
+ *
&N
code for "total pages"
+ *
&font size
code for "text font size", where font size is a font size in points.
+ *
&K
code for "text font color" + * RGB Color is specified as RRGGBB + * Theme Color is specifed as TTSNN where TT is the theme color Id, S is either "+" or "-" of the tint/shade + * value, NN is the tint/shade value.
+ *
&S
code for "text strikethrough" on / off
+ *
&X
code for "text super script" on / off
+ *
&Y
code for "text subscript" on / off
+ *
&C
code for "center section". When two or more occurrences of this section marker exist, the contents + * from all markers are concatenated, in the order of appearance, and placed into the center section. + * SpreadsheetML Reference Material - Worksheets 1966
+ *
&D
code for "date"
+ *
&T
code for "time"
+ *
&G
code for "picture as background"
+ *
&U
code for "text single underline"
+ *
&E
code for "double underline"
+ *
&R
code for "right section". When two or more occurrences of this section marker exist, the contents + * from all markers are concatenated, in the order of appearance, and placed into the right section.
+ *
&Z
code for "this workbook's file path"
+ *
&F
code for "this workbook's file name"
+ *
&A
code for "sheet tab name"
+ *
&+
code for add to page #.
+ *
&-
code for subtract from page #.
+ *
&"font name,font type" - code for "text font name" and "text font type", where font name and font type + * are strings specifying the name and type of the font, separated by a comma. When a hyphen appears in font + * name, it means "none specified". Both of font name and font type can be localized values. + *
&"-,Bold"
code for "bold font style"
+ *
&B
also means "bold font style"
+ *
&"-,Regular"
code for "regular font style"
+ *
&"-,Italic"
code for "italic font style"
+ *
&I
also means "italic font style"
+ *
&"-,Bold Italic"
code for "bold italic font style"
+ *
&O
code for "outline style"
+ *
&H
code for "shadow style"
+ *
+ * + * */ public abstract class XSSFHeaderFooter implements HeaderFooter { private HeaderFooterHelper helper; private CTHeaderFooter headerFooter; private boolean stripFields = false; - + + /** + * Create an instance of XSSFHeaderFooter from the supplied XML bean + * @param headerFooter + */ public XSSFHeaderFooter(CTHeaderFooter headerFooter) { this.headerFooter = headerFooter; this.helper = new HeaderFooterHelper(); } + /** + * Returns the underlying CTHeaderFooter xml bean + * + * @return the underlying CTHeaderFooter xml bean + */ public CTHeaderFooter getHeaderFooter() { return this.headerFooter; } + /** + * + * @return + */ public String getValue() { String value = getText(); if(value == null) @@ -69,7 +140,14 @@ public void setAreFieldsStripped(boolean stripFields) { this.stripFields = stripFields; } - + + /** + * Removes any fields (eg macros, page markers etc) + * from the string. + * Normally used to make some text suitable for showing + * to humans, and the resultant text should not normally + * be saved back into the document! + */ public static String stripFields(String text) { return org.apache.poi.hssf.usermodel.HeaderFooter.stripFields(text); } @@ -79,6 +157,9 @@ protected abstract void setText(String text); + /** + * get the text representing the center part of this element + */ public String getCenter() { String text = helper.getCenterSection(getText()); if(stripFields) @@ -86,6 +167,9 @@ return text; } + /** + * get the text representing the left part of this element + */ public String getLeft() { String text = helper.getLeftSection(getText()); if(stripFields) @@ -93,6 +177,9 @@ return text; } + /** + * get the text representing the right part of this element + */ public String getRight() { String text = helper.getRightSection(getText()); if(stripFields) @@ -100,14 +187,23 @@ return text; } + /** + * set a centered string value for this element + */ public void setCenter(String newCenter) { setText(helper.setCenterSection(getText(), newCenter)); } + /** + * set a left string value for this element + */ public void setLeft(String newLeft) { setText(helper.setLeftSection(getText(), newLeft)); } + /** + * set a right string value for this element + */ public void setRight(String newRight) { setText(helper.setRightSection(getText(), newRight)); } Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java (working copy) @@ -21,16 +21,39 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + * + * Even page footer value. Corresponds to even printed pages. + * Even page(s) in the sheet may not be printed, for example, if the print area is specified to be + * a range such that it falls outside an even page's scope. + * If no even footer is specified, then the odd footer's value is assumed for even page footers. + * + */ public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{ - + + /** + * Create an instance of XSSFEvenFooter from the supplied XML bean + * @see XSSFSheet#getEvenFooter() + * @param headerFooter + */ public XSSFEvenFooter(CTHeaderFooter headerFooter) { super(headerFooter); + headerFooter.setDifferentOddEven(true); } + /** + * Get the content text representing the footer + * @return text + */ public String getText() { return getHeaderFooter().getEvenFooter(); } + /** + * Set a text for the footer. If null unset the value. + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the footer. + */ public void setText(String text) { if(text == null) { getHeaderFooter().unsetEvenFooter(); Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java (working copy) @@ -21,16 +21,36 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + * Odd page header value. Corresponds to odd printed pages. + * Odd page(s) in the sheet may not be printed, for example, if the print area is specified to be + * a range such that it falls outside an odd page's scope. + * + */ public class XSSFOddHeader extends XSSFHeaderFooter implements Header{ + /** + * Create an instance of XSSFOddHeader from the supplied XML bean + * @see XSSFSheet#getOddHeader() + * @param headerFooter + */ protected XSSFOddHeader(CTHeaderFooter headerFooter) { super(headerFooter); } + /** + * Get the content text representing this header + * @return text + */ public String getText() { return getHeaderFooter().getOddHeader(); } + /** + * Set a text for the header. If null unset the value + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the header. + */ public void setText(String text) { if(text == null) { getHeaderFooter().unsetOddHeader(); Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java (working copy) @@ -21,16 +21,38 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + * + * First page header content. Corresponds to first printed page. + * The first logical page in the sheet may not be printed, for example, if the print area is specified to + * be a range such that it falls outside the first page's scope. + * + */ public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{ + /** + * Create an instance of XSSFFirstHeader from the supplied XML bean + * @see XSSFSheet#getFirstHeader() + * @param headerFooter + */ protected XSSFFirstHeader(CTHeaderFooter headerFooter) { super(headerFooter); + headerFooter.setDifferentFirst(true); } + /** + * Get the content text representing this header + * @return text + */ public String getText() { return getHeaderFooter().getFirstHeader(); } + /** + * Set a text for the header. If null unset the value + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the header. + */ public void setText(String text) { if(text == null) { getHeaderFooter().unsetFirstHeader(); Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java (working copy) @@ -21,22 +21,47 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + *

+ * Even page header value. Corresponds to even printed pages. + * Even page(s) in the sheet may not be printed, for example, if the print area is specified to be + * a range such that it falls outside an even page's scope. + * If no even header is specified, then odd header value is assumed for even page headers. + *

+ * + */ public class XSSFEvenHeader extends XSSFHeaderFooter implements Header{ + /** + * Create an instance of XSSFEvenHeader from the supplied XML bean + * @see XSSFSheet#getEvenHeader() + * @param headerFooter + */ public XSSFEvenHeader(CTHeaderFooter headerFooter) { - super(headerFooter); + super(headerFooter); + headerFooter.setDifferentOddEven(true); } - + + /** + * Get the content text representing this header + * @return text + */ public String getText() { - return getHeaderFooter().getEvenHeader(); + return getHeaderFooter().getEvenHeader(); } - + + /** + * Set a text for the header. If null unset the value + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the header. + */ public void setText(String text) { - if(text == null) { - getHeaderFooter().unsetEvenHeader(); - } else { - getHeaderFooter().setEvenHeader(text); - } + if(text == null) { + getHeaderFooter().unsetEvenHeader(); + } else { + getHeaderFooter().setEvenHeader(text); + } } + } Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java (revision 708214) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java (working copy) @@ -21,16 +21,36 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +/** + * Odd page footer value. Corresponds to odd printed pages. + * Odd page(s) in the sheet may not be printed, for example, if the print area is specified to be + * a range such that it falls outside an odd page's scope. + * + */ public class XSSFOddFooter extends XSSFHeaderFooter implements Footer{ + /** + * Create an instance of XSSFOddFooter from the supplied XML bean + * @see XSSFSheet#getOddFooter() + * @param headerFooter + */ public XSSFOddFooter(CTHeaderFooter headerFooter) { super(headerFooter); } + /** + * Get the content text representing the footer + * @return text + */ public String getText() { return getHeaderFooter().getOddFooter(); } + /** + * Set a text for the footer. If null unset the value. + * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax + * @param text - a string representing the footer. + */ public void setText(String text) { if(text == null) { getHeaderFooter().unsetOddFooter(); Index: src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java =================================================================== --- src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java (revision 708214) +++ src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java (working copy) @@ -19,8 +19,10 @@ import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Footer; +import org.apache.poi.ss.usermodel.Header; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class HeadersAndFooters { @@ -28,7 +30,7 @@ public static void main(String[]args) throws Exception { Workbook wb = new XSSFWorkbook(); - Sheet sheet = wb.createSheet("format sheet"); + Sheet sheet = wb.createSheet("first-header - format sheet"); sheet.createRow(0).createCell(0).setCellValue(123); //set page numbers in the footer @@ -37,9 +39,44 @@ //&N == page numbers footer.setRight("Page &P of &N"); - // Create various cells and rows for spreadsheet. + + Header firstHeader=((XSSFSheet)sheet).getFirstHeader(); + //&F == workbook file name + firstHeader.setLeft("&F ......... first header"); + + for(int i=0;i<100;i=i+10){ + sheet.createRow(i).createCell(0).setCellValue(123); + } + + + XSSFSheet sheet2 = (XSSFSheet)wb.createSheet("odd header-even footer"); + Header oddHeader=sheet2.getOddHeader(); + //&B == bold + //&E == double underline + //&D == date + oddHeader.setCenter("&B &E oddHeader &D "); + + Footer evenFooter=sheet2.getEvenFooter(); + evenFooter.setRight("even footer &P"); + sheet2.createRow(10).createCell(0).setCellValue("Second sheet with an oddHeader and an evenFooter"); - FileOutputStream fileOut = new FileOutputStream("pageNumerOnFooter.xlsx"); + for(int i=0;i<200;i=i+10){ + sheet2.createRow(i).createCell(0).setCellValue(123); + } + + XSSFSheet sheet3 = (XSSFSheet)wb.createSheet("odd header- odd footer"); + sheet3.createRow(10).createCell(0).setCellValue("Third sheet with oddHeader and oddFooter"); + Header oddH=sheet3.getOddHeader(); + //&C == centered + oddH.setCenter("centered oddHeader"); + oddH.setLeft("left "); + oddH.setRight("right "); + + Footer oddF=sheet3.getOddFooter(); + oddF.setLeft("Page &P"); + oddF.setRight("Pages &N "); + + FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx"); wb.write(fileOut); fileOut.close();