From 62a42982922ad2bab018b5ee5fcf81ac567a51c1 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 30 Oct 2008 09:42:53 +0000 Subject: [PATCH] applied patches #46119, #46120 and #46078 by Gizella Bronzetti git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@709126 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/examples/HeadersAndFooters.java | 43 +++++++- .../xssf/usermodel/examples/ShiftRows.java | 61 ++++++++++ .../examples/SplitAndFreezePanes.java | 50 +++++++++ .../apache/poi/xssf/usermodel/XSSFCell.java | 14 +++ .../poi/xssf/usermodel/XSSFEvenFooter.java | 25 ++++- .../poi/xssf/usermodel/XSSFEvenHeader.java | 51 ++++++--- .../poi/xssf/usermodel/XSSFFirstFooter.java | 22 ++++ .../poi/xssf/usermodel/XSSFFirstHeader.java | 22 ++++ .../poi/xssf/usermodel/XSSFOddFooter.java | 20 ++++ .../poi/xssf/usermodel/XSSFOddHeader.java | 20 ++++ .../apache/poi/xssf/usermodel/XSSFRow.java | 13 ++- .../apache/poi/xssf/usermodel/XSSFSheet.java | 15 +++ .../extensions/XSSFHeaderFooter.java | 104 +++++++++++++++++- 13 files changed, 433 insertions(+), 27 deletions(-) create mode 100755 src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java create mode 100755 src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java index 8c69500e9..3726c5819 100755 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java @@ -19,8 +19,10 @@ package org.apache.poi.xssf.usermodel.examples; 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 class HeadersAndFooters { 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 @@ public class HeadersAndFooters { //&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(); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java new file mode 100755 index 000000000..09ae38690 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java @@ -0,0 +1,61 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel.examples; + +import java.io.FileOutputStream; + +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +/** + * How to shift rows up or down + */ +public class ShiftRows { + + public static void main(String[]args) throws Exception { + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); + + Row row1 = sheet.createRow(1); + row1.createCell(0).setCellValue(1); + + Row row2 = sheet.createRow(4); + row2.createCell(1).setCellValue(2); + + Row row3 = sheet.createRow(5); + row3.createCell(2).setCellValue(3); + + Row row4 = sheet.createRow(6); + row4.createCell(3).setCellValue(4); + + Row row5 = sheet.createRow(9); + row5.createCell(4).setCellValue(5); + + // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) + sheet.shiftRows(5, 10, -4); + + FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx"); + wb.write(fileOut); + fileOut.close(); + + } + + +} diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java new file mode 100755 index 000000000..5eaeeee86 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java @@ -0,0 +1,50 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.usermodel.examples; + +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileOutputStream; + +/** + * How to set spklit and freeze panes + */ +public class SplitAndFreezePanes { + public static void main(String[]args) throws Exception { + Workbook wb = new XSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); + Sheet sheet3 = wb.createSheet("third sheet"); + Sheet sheet4 = wb.createSheet("fourth sheet"); + + // Freeze just one row + sheet1.createFreezePane(0, 1, 0, 1); + // Freeze just one column + sheet2.createFreezePane(1, 0, 1, 0); + // Freeze the columns and rows (forget about scrolling position of the lower right quadrant). + sheet3.createFreezePane(2, 2); + // Create a split with the lower left side being the active quadrant + sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT); + + FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx"); + wb.write(fileOut); + fileOut.close(); + + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 926b41ee5..f738b8774 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -809,4 +809,18 @@ public final class XSSFCell implements Cell { public CTCell getCTCell(){ return cell; } + + /** + * update cell reference when shifting rows + * + * @param row + */ + protected void modifyCellReference(XSSFRow row) { + this.cell.setR(new CellReference(row.getRowNum(), cellNum).formatAsString()); + + CTCell[] ctCells = row.getCTRow().getCArray(); + for (CTCell ctCell : ctCells) { + ctCell.setR(new CellReference(row.getRowNum(), cellNum).formatAsString()); + } + } } 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 759b3e4d2..9dd28daf2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java @@ -21,16 +21,39 @@ import org.apache.poi.ss.usermodel.Footer; 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(); 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 0a63110ba..1bf9555fc 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java @@ -21,22 +21,47 @@ import org.apache.poi.ss.usermodel.Header; 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); - } - - public String getText() { - return getHeaderFooter().getEvenHeader(); - } - - public void setText(String text) { - if(text == null) { - getHeaderFooter().unsetEvenHeader(); - } else { - getHeaderFooter().setEvenHeader(text); - } + super(headerFooter); + headerFooter.setDifferentOddEven(true); } + /** + * Get the content text representing this header + * @return text + */ + public String getText() { + 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); + } + } + + } 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 bd5c0f1f9..b0f2cb517 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java @@ -21,16 +21,38 @@ import org.apache.poi.ss.usermodel.Footer; 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(); 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 9cd404485..9ffd45781 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java @@ -21,16 +21,38 @@ import org.apache.poi.ss.usermodel.Header; 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(); 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 8e3143b0d..ca3950f81 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java @@ -21,16 +21,36 @@ import org.apache.poi.ss.usermodel.Footer; 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(); 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 5f1aa9c03..ba5c1a06f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java @@ -21,16 +21,36 @@ import org.apache.poi.ss.usermodel.Header; 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(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index 610c1f3c6..792e14ca8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -267,22 +267,23 @@ public class XSSFRow implements Row, Comparable { * @param height the height in "twips" or 1/20th of a point. -1 resets to the default height */ public void setHeight(short height) { - if(height == -1){ - this.row.unsetHt(); - this.row.unsetCustomHeight(); + if (height == -1) { + if (row.isSetHt()) row.unsetHt(); + if (row.isSetCustomHeight()) row.unsetCustomHeight(); } else { - this.row.setHt((double)height/20); - this.row.setCustomHeight(true); + row.setHt((double) height / 20); + row.setCustomHeight(true); } } + /** * Set the row's height in points. * * @param height the height in points. -1 resets to the default height */ public void setHeightInPoints(float height) { - setHeight((short)(height*20)); + setHeight((short)(height == -1 ? -1 : (height*20))); } /** 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 6174f6d6b..13c1ffce5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -1412,9 +1412,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { for (Iterator it = rowIterator() ; it.hasNext() ; ) { Row row = it.next(); + if (!copyRowHeight) { row.setHeight((short)-1); } + if (resetOriginalRowHeight && getDefaultRowHeight() >= 0) { row.setHeight(getDefaultRowHeight()); } @@ -1423,6 +1425,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } else if (row.getRowNum() >= startRow && row.getRowNum() <= endRow) { row.setRowNum(row.getRowNum() + n); + if (row.getFirstCellNum() > -1) { + modifyCellReference((XSSFRow) row); + } } } //rebuild the rows map @@ -1431,6 +1436,16 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { rows = map; } + + private void modifyCellReference(XSSFRow row) { + for (int i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) { + XSSFCell c = row.getCell(i); + if (c != null) { + c.modifyCellReference(row); + } + } + } + /** * Location of the top left visible cell Location of the top left visible cell in the bottom right * pane (when in Left-to-Right mode). diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java index 5a9e4cda0..dd708d79b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java @@ -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 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; * * 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 abstract class XSSFHeaderFooter implements HeaderFooter { 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 @@ public abstract class XSSFHeaderFooter implements HeaderFooter { 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 @@ public abstract class XSSFHeaderFooter implements HeaderFooter { 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 @@ public abstract class XSSFHeaderFooter implements HeaderFooter { 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 @@ public abstract class XSSFHeaderFooter implements HeaderFooter { 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)); }