+/* ====================================================================
+ 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;
public class FitSheetToOnePage {
- public static void main(String[]args) throws Exception{
- Workbook wb = new XSSFWorkbook();
- Sheet sheet = wb.createSheet("format sheet");
- PrintSetup ps = sheet.getPrintSetup();
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet("format sheet");
+ PrintSetup ps = sheet.getPrintSetup();
- sheet.setAutobreaks(true);
+ sheet.setAutobreaks(true);
- ps.setFitHeight((short)1);
- ps.setFitWidth((short)1);
+ ps.setFitHeight((short) 1);
+ ps.setFitWidth((short) 1);
+ // Create various cells and rows for spreadsheet.
- // Create various cells and rows for spreadsheet.
-
- FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx");
- wb.write(fileOut);
- fileOut.close();
+ FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
}
}
--- /dev/null
+/* ====================================================================
+ 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.Footer;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+public class HeadersAndFooters {
+
+
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet("format sheet");
+ sheet.createRow(0).createCell(0).setCellValue(123);
+
+ //set page numbers in the footer
+ Footer footer = sheet.getFooter();
+ //&P == current page number
+ //&N == page numbers
+ footer.setRight("Page &P of &N");
+
+ // Create various cells and rows for spreadsheet.
+
+ FileOutputStream fileOut = new FileOutputStream("pageNumerOnFooter.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.usermodel.IndexedColors;
+
+/**
+ * Demonstrates how to create hyperlinks.
+ */
+public class HyperlinkExample {
+
+
+ public static void main(String[]args) throws Exception{
+ Workbook wb = new XSSFWorkbook();
+ CreationHelper createHelper = wb.getCreationHelper();
+
+ //cell style for hyperlinks
+ //by default hypelrinks are blue and underlined
+ CellStyle hlink_style = wb.createCellStyle();
+ Font hlink_font = wb.createFont();
+ hlink_font.setUnderline(Font.U_SINGLE);
+ hlink_font.setColor(IndexedColors.BLUE.getIndex());
+ hlink_style.setFont(hlink_font);
+
+ Cell cell;
+ Sheet sheet = wb.createSheet("Hyperlinks");
+ //URL
+ cell = sheet.createRow(0).createCell((short)0);
+ cell.setCellValue("URL Link");
+
+ Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
+ link.setAddress("http://poi.apache.org/");
+ cell.setHyperlink(link);
+ cell.setCellStyle(hlink_style);
+
+ //link to a file in the current directory
+ cell = sheet.createRow(1).createCell((short)0);
+ cell.setCellValue("File Link");
+ link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
+ link.setAddress("link1.xls");
+ cell.setHyperlink(link);
+ cell.setCellStyle(hlink_style);
+
+ //e-mail link
+ cell = sheet.createRow(2).createCell((short)0);
+ cell.setCellValue("Email Link");
+ link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
+ //note, if subject contains white spaces, make sure they are url-encoded
+ link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
+ cell.setHyperlink(link);
+ cell.setCellStyle(hlink_style);
+
+ //link to a place in this workbook
+
+ //create a target sheet and cell
+ Sheet sheet2 = wb.createSheet("Target Sheet");
+ sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
+
+ cell = sheet.createRow(3).createCell((short)0);
+ cell.setCellValue("Worksheet Link");
+ Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
+ link2.setAddress("'Target Sheet'!A1");
+ cell.setHyperlink(link2);
+ cell.setCellStyle(hlink_style);
+
+ FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
+ wb.write(out);
+ out.close();
+
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+
+public class SelectedSheet {
+
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet("row sheet");
+
+ Sheet sheet2 = wb.createSheet("another sheet");
+ Sheet sheet3 = wb.createSheet(" sheet 3 ");
+ sheet3.setSelected(true);
+ wb.setActiveSheet(2);
+
+ // Create various cells and rows for spreadsheet.
+
+ FileOutputStream fileOut = new FileOutputStream("selectedSheet.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+
+}
public String getLabel() {
return ctHyperlink.getDisplay();
}
+ public String getLocation() {
+ return ctHyperlink.getLocation();
+ }
public void setLabel(String label) {
ctHyperlink.setDisplay(label);
}
+
+ public void setLocation(String location){
+ ctHyperlink.setLocation(location);
+ }
+
public void setAddress(String address) {
location = address;
- }
+ //we must set location for internal hyperlinks
+ if(type == Hyperlink.LINK_DOCUMENT){
+ setLocation(address);
+ }
+ }
/**
* Assigns this hyperlink to the given cell reference
private CTCellAlignment cellAlignement;
+ /**
+ * Creates a Cell Alignment from the supplied XML definition
+ *
+ * @param cellAlignment
+ */
public XSSFCellAlignment(CTCellAlignment cellAlignment) {
this.cellAlignement = cellAlignment;
}
+ /**
+ * Get the type of vertical alignment for the cell
+ *
+ * @return the type of aligment
+ * @see VerticalAlignment
+ */
public VerticalAlignment getVertical() {
STVerticalAlignment.Enum align = cellAlignement.getVertical();
- if(align == null) align = STVerticalAlignment.BOTTOM;
+ if (align == null) align = STVerticalAlignment.BOTTOM;
return VerticalAlignment.values()[align.intValue() - 1];
}
- public void setVertical(VerticalAlignment vertical) {
- cellAlignement.setVertical(STVerticalAlignment.Enum.forInt(vertical.ordinal() + 1));
+ /**
+ * Set the type of vertical alignment for the cell
+ *
+ * @param align - the type of alignment
+ * @see VerticalAlignment
+ */
+ public void setVertical(VerticalAlignment align) {
+ cellAlignement.setVertical(STVerticalAlignment.Enum.forInt(align.ordinal() + 1));
}
+ /**
+ * Get the type of horizontal alignment for the cell
+ *
+ * @return the type of aligment
+ * @see HorizontalAlignment
+ */
public HorizontalAlignment getHorizontal() {
STHorizontalAlignment.Enum align = cellAlignement.getHorizontal();
- if(align == null) align = STHorizontalAlignment.GENERAL;
+ if (align == null) align = STHorizontalAlignment.GENERAL;
return HorizontalAlignment.values()[align.intValue() - 1];
}
+ /**
+ * Set the type of horizontal alignment for the cell
+ *
+ * @param align - the type of alignment
+ * @see HorizontalAlignment
+ */
public void setHorizontal(HorizontalAlignment align) {
cellAlignement.setHorizontal(STHorizontalAlignment.Enum.forInt(align.ordinal() + 1));
}
+ /**
+ * Get the number of spaces to indent the text in the cell
+ *
+ * @return indent - number of spaces
+ */
public long getIndent() {
return cellAlignement.getIndent();
}
+ /**
+ * Set the number of spaces to indent the text in the cell
+ *
+ * @param indent - number of spaces
+ */
public void setIndent(long indent) {
cellAlignement.setIndent(indent);
}
+ /**
+ * Get the degree of rotation for the text in the cell
+ * <p/>
+ * Expressed in degrees. Values range from 0 to 180. The first letter of
+ * the text is considered the center-point of the arc.
+ * <br/>
+ * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
+ * horizon is calculated as:
+ * <br/>
+ * <code>[degrees below horizon] = 90 - textRotation.</code>
+ * </p>
+ *
+ * @return rotation degrees (between 0 and 180 degrees)
+ */
public long getTextRotation() {
return cellAlignement.getTextRotation();
}
+ /**
+ * Set the degree of rotation for the text in the cell
+ * <p/>
+ * Expressed in degrees. Values range from 0 to 180. The first letter of
+ * the text is considered the center-point of the arc.
+ * <br/>
+ * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
+ * horizon is calculated as:
+ * <br/>
+ * <code>[degrees below horizon] = 90 - textRotation.</code>
+ * </p>
+ *
+ * @param rotation - the rotation degrees (between 0 and 180 degrees)
+ */
public void setTextRotation(long rotation) {
cellAlignement.setTextRotation(rotation);
}
+ /**
+ * Whether the text should be wrapped
+ *
+ * @return a boolean value indicating if the text in a cell should be line-wrapped within the cell.
+ */
public boolean getWrapText() {
return cellAlignement.getWrapText();
}
+ /**
+ * Set whether the text should be wrapped
+ *
+ * @param wrapped a boolean value indicating if the text in a cell should be line-wrapped within the cell.
+ */
public void setWrapText(boolean wrapped) {
cellAlignement.setWrapText(wrapped);
}
package org.apache.poi.xssf.usermodel.extensions;
-import java.util.LinkedList;
-
+import org.apache.poi.xssf.usermodel.BorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
-import org.apache.poi.xssf.usermodel.BorderStyle;
-
+/**
+ * This element contains border formatting information, specifying border definition formats (left, right, top, bottom, diagonal)
+ * for cells in the workbook.
+ * Color is optional.
+ */
public class XSSFCellBorder {
private CTBorder border;
public XSSFCellBorder(CTBorder border) {
this.border = border;
}
+
/**
* Creates a new, empty Cell Border, on the
- * given Styles Table
+ * given Styles Table
*/
public XSSFCellBorder() {
border = CTBorder.Factory.newInstance();
}
+ /**
+ * The enumeration value indicating the side being used for a cell border.
+ */
public static enum BorderSide {
TOP, RIGHT, BOTTOM, LEFT
}
+ /**
+ * Returns the underlying XML bean.
+ *
+ * @return CTBorder
+ */
public CTBorder getCTBorder() {
return border;
}
+ /**
+ * Get the type of border to use for the selected border
+ *
+ * @param side - - where to apply the color definition
+ * @return borderstyle - the type of border to use. default value is NONE if border style is not set.
+ * @see BorderStyle
+ */
public BorderStyle getBorderStyle(BorderSide side) {
CTBorderPr ctBorder = getBorder(side);
STBorderStyle.Enum border = ctBorder == null ? STBorderStyle.NONE : ctBorder.getStyle();
return BorderStyle.values()[border.intValue() - 1];
}
+ /**
+ * Set the type of border to use for the selected border
+ *
+ * @param side - - where to apply the color definition
+ * @param style - border style
+ * @see BorderStyle
+ */
public void setBorderStyle(BorderSide side, BorderStyle style) {
getBorder(side, true).setStyle(STBorderStyle.Enum.forInt(style.ordinal() + 1));
}
+ /**
+ * Get the color to use for the selected border
+ *
+ * @param side - where to apply the color definition
+ * @return color - color to use as XSSFColor. null if color is not set
+ */
public XSSFColor getBorderColor(BorderSide side) {
CTBorderPr borderPr = getBorder(side);
return borderPr != null && borderPr.isSetColor() ?
new XSSFColor(borderPr.getColor()) : null;
}
+ /**
+ * Set the color to use for the selected border
+ *
+ * @param side - where to apply the color definition
+ * @param color - the color to use
+ */
public void setBorderColor(BorderSide side, XSSFColor color) {
CTBorderPr borderPr = getBorder(side, true);
- if(color == null) borderPr.unsetColor();
- else borderPr.setColor(color.getCTColor());
+ if (color == null) borderPr.unsetColor();
+ else
+ borderPr.setColor(color.getCTColor());
}
private CTBorderPr getBorder(BorderSide side) {
return getBorder(side, false);
}
+
private CTBorderPr getBorder(BorderSide side, boolean ensure) {
CTBorderPr borderPr;
switch (side) {
case TOP:
borderPr = border.getTop();
- if(ensure && borderPr == null) borderPr = border.addNewTop();
+ if (ensure && borderPr == null) borderPr = border.addNewTop();
break;
case RIGHT:
borderPr = border.getRight();
- if(ensure && borderPr == null) borderPr = border.addNewRight();
+ if (ensure && borderPr == null) borderPr = border.addNewRight();
break;
case BOTTOM:
borderPr = border.getBottom();
- if(ensure && borderPr == null) borderPr = border.addNewBottom();
+ if (ensure && borderPr == null) borderPr = border.addNewBottom();
break;
case LEFT:
borderPr = border.getLeft();
- if(ensure && borderPr == null) borderPr = border.addNewLeft();
+ if (ensure && borderPr == null) borderPr = border.addNewLeft();
break;
default:
throw new IllegalArgumentException("No suitable side specified for the border");
}
- public int hashCode(){
+ public int hashCode() {
return border.toString().hashCode();
}
- public boolean equals(Object o){
- if(!(o instanceof XSSFCellBorder)) return false;
+ public boolean equals(Object o) {
+ if (!(o instanceof XSSFCellBorder)) return false;
- XSSFCellBorder cf = (XSSFCellBorder)o;
+ XSSFCellBorder cf = (XSSFCellBorder) o;
return border.toString().equals(cf.getCTBorder().toString());
}
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
-import java.util.List;
-
-import org.apache.poi.xssf.usermodel.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
+/**
+ * This element specifies fill formatting.
+ * A cell fill consists of a background color, foreground color, and pattern to be applied across the cell.
+ */
public final class XSSFCellFill {
-
- private CTFill _fill;
-
- public XSSFCellFill(CTFill fill) {
- _fill = fill;
- }
-
- public XSSFCellFill() {
- _fill = CTFill.Factory.newInstance();
- }
-
- public XSSFColor getFillBackgroundColor() {
+
+ private CTFill _fill;
+
+ /**
+ * Creates a CellFill from the supplied parts
+ *
+ * @param fill - fill
+ */
+ public XSSFCellFill(CTFill fill) {
+ _fill = fill;
+ }
+
+ /**
+ * Creates an empty CellFill
+ */
+ public XSSFCellFill() {
+ _fill = CTFill.Factory.newInstance();
+ }
+
+ /**
+ * Get the background fill color.
+ *
+ * @return fill color, null if color is not set
+ */
+ public XSSFColor getFillBackgroundColor() {
CTPatternFill ptrn = _fill.getPatternFill();
- if(ptrn == null) return null;
+ if (ptrn == null) return null;
CTColor ctColor = ptrn.getBgColor();
- return ctColor == null ? null : new XSSFColor(ctColor);
- }
+ return ctColor == null ? null : new XSSFColor(ctColor);
+ }
+ /**
+ * Set the background fill color represented as a indexed color value.
+ *
+ * @param index
+ */
public void setFillBackgroundColor(int index) {
CTPatternFill ptrn = ensureCTPatternFill();
CTColor ctColor = ptrn.isSetBgColor() ? ptrn.getBgColor() : ptrn.addNewBgColor();
ctColor.setIndexed(index);
}
+ /**
+ * Set the background fill color represented as a {@link XSSFColor} value.
+ *
+ * @param color
+ */
public void setFillBackgroundColor(XSSFColor color) {
CTPatternFill ptrn = ensureCTPatternFill();
ptrn.setBgColor(color.getCTColor());
}
+ /**
+ * Get the foreground fill color.
+ *
+ * @return XSSFColor - foreground color. null if color is not set
+ */
public XSSFColor getFillForegroundColor() {
CTPatternFill ptrn = _fill.getPatternFill();
- if(ptrn == null) return null;
+ if (ptrn == null) return null;
CTColor ctColor = ptrn.getFgColor();
return ctColor == null ? null : new XSSFColor(ctColor);
}
+ /**
+ * Set the foreground fill color as a indexed color value
+ *
+ * @param index - the color to use
+ */
public void setFillForegroundColor(int index) {
CTPatternFill ptrn = ensureCTPatternFill();
CTColor ctColor = ptrn.isSetFgColor() ? ptrn.getFgColor() : ptrn.addNewFgColor();
ctColor.setIndexed(index);
}
+ /**
+ * Set the foreground fill color represented as a {@link XSSFColor} value.
+ *
+ * @param color - the color to use
+ */
public void setFillForegroundColor(XSSFColor color) {
CTPatternFill ptrn = ensureCTPatternFill();
ptrn.setFgColor(color.getCTColor());
}
- public STPatternType.Enum getPatternType() {
+ /**
+ * get the fill pattern
+ *
+ * @return fill pattern type. null if fill pattern is not set
+ */
+ public STPatternType.Enum getPatternType() {
CTPatternFill ptrn = _fill.getPatternFill();
- return ptrn == null ? null : ptrn.getPatternType();
- }
+ return ptrn == null ? null : ptrn.getPatternType();
+ }
+ /**
+ * set the fill pattern
+ *
+ * @param patternType fill pattern to use
+ */
public void setPatternType(STPatternType.Enum patternType) {
CTPatternFill ptrn = ensureCTPatternFill();
ptrn.setPatternType(patternType);
}
- private CTPatternFill ensureCTPatternFill() {
- CTPatternFill patternFill = _fill.getPatternFill();
- if (patternFill == null) {
- patternFill = _fill.addNewPatternFill();
- }
- return patternFill;
- }
-
- public CTFill getCTFill() {
- return _fill;
- }
-
- public int hashCode(){
+ private CTPatternFill ensureCTPatternFill() {
+ CTPatternFill patternFill = _fill.getPatternFill();
+ if (patternFill == null) {
+ patternFill = _fill.addNewPatternFill();
+ }
+ return patternFill;
+ }
+
+ /**
+ * Returns the underlying XML bean.
+ *
+ * @return CTFill
+ */
+ public CTFill getCTFill() {
+ return _fill;
+ }
+
+
+ public int hashCode() {
return _fill.toString().hashCode();
}
- public boolean equals(Object o){
- if(!(o instanceof XSSFCellFill)) return false;
+ public boolean equals(Object o) {
+ if (!(o instanceof XSSFCellFill)) return false;
- XSSFCellFill cf = (XSSFCellFill)o;
+ XSSFCellFill cf = (XSSFCellFill) o;
return _fill.toString().equals(cf.getCTFill().toString());
}
}