applied patches provided by Gisella Bronzetti in bugs #45918, #45917 and #45920

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@700844 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-10-01 17:15:02 +00:00
parent 0afe659835
commit 0d051eb3c2
7 changed files with 1026 additions and 608 deletions

View File

@ -276,6 +276,7 @@ public class DateUtil {
double d = cell.getNumericCellValue(); double d = cell.getNumericCellValue();
if ( DateUtil.isValidExcelDate(d) ) { if ( DateUtil.isValidExcelDate(d) ) {
CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
if(style==null) return false;
int i = style.getDataFormat(); int i = style.getDataFormat();
String f = style.getDataFormatString(); String f = style.getDataFormatString();
bDate = isADateFormat(i, f); bDate = isADateFormat(i, f);

View File

@ -17,10 +17,13 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.Comment;
@ -73,6 +76,7 @@ public final class XSSFCell implements Cell {
protected SharedStringSource getSharedStringSource() { protected SharedStringSource getSharedStringSource() {
return this.sharedStringSource; return this.sharedStringSource;
} }
protected StylesSource getStylesSource() { protected StylesSource getStylesSource() {
return this.stylesSource; return this.stylesSource;
} }
@ -154,10 +158,10 @@ public final class XSSFCell implements Cell {
if (STCellType.N == this.cell.getT() || STCellType.STR == this.cell.getT()) { if (STCellType.N == this.cell.getT() || STCellType.STR == this.cell.getT()) {
double value = this.getNumericCellValue(); double value = this.getNumericCellValue();
if (false /* book.isUsing1904DateWindowing() */) { // FIXME if (false /* book.isUsing1904DateWindowing() */) { // FIXME
return HSSFDateUtil.getJavaDate(value,true); return DateUtil.getJavaDate(value,true);
} }
else { else {
return HSSFDateUtil.getJavaDate(value,false); return DateUtil.getJavaDate(value,false);
} }
} }
throw new NumberFormatException("You cannot get a date value from a cell of type " + this.cell.getT()); throw new NumberFormatException("You cannot get a date value from a cell of type " + this.cell.getT());
@ -256,14 +260,17 @@ public final class XSSFCell implements Cell {
} }
throw new NumberFormatException("You cannot get a string value from a non-string cell"); throw new NumberFormatException("You cannot get a string value from a non-string cell");
} }
/**
* Sets this cell as the active cell for the worksheet
*/
public void setAsActiveCell() { public void setAsActiveCell() {
row.getSheet().setActiveCell(cell.getR()); row.getSheet().setActiveCell(cell.getR());
} }
public void setCellComment(Comment comment) { public void setCellComment(Comment comment) {
String cellRef = String cellRef = new CellReference(row.getRowNum(), getCellNum()).formatAsString();
new CellReference(row.getRowNum(), getCellNum()).formatAsString();
row.getSheet().setCellComment(cellRef, (XSSFComment)comment); row.getSheet().setCellComment(cellRef, (XSSFComment)comment);
} }
@ -352,6 +359,15 @@ public final class XSSFCell implements Cell {
} }
} }
/**
* set the cells type (numeric, formula or string)
* @see #CELL_TYPE_NUMERIC
* @see #CELL_TYPE_STRING
* @see #CELL_TYPE_FORMULA
* @see #CELL_TYPE_BLANK
* @see #CELL_TYPE_BOOLEAN
* @see #CELL_TYPE_ERROR
*/
public void setCellType(int cellType) { public void setCellType(int cellType) {
switch (cellType) { switch (cellType) {
case CELL_TYPE_BOOLEAN: case CELL_TYPE_BOOLEAN:
@ -379,13 +395,38 @@ public final class XSSFCell implements Cell {
this.cell.setV(String.valueOf(value)); this.cell.setV(String.valueOf(value));
} }
/**
* set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
* a date.
*
* @param value the date value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For other types we
* will change the cell to a numeric cell and set its value.
*/
public void setCellValue(Date value) { public void setCellValue(Date value) {
setCellValue(HSSFDateUtil.getExcelDate(value, false /*this.book.isUsing1904DateWindowing()*/)); // FIXME boolean date1904 = this.row.getSheet().getWorkbook().isDate1904();
setCellValue(DateUtil.getExcelDate(value, date1904));
} }
/**
* set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
* a date.
*
* This will set the cell value based on the Calendar's timezone. As Excel
* does not support timezones this means that both 20:00+03:00 and
* 20:00-03:00 will be reported as the same value (20:00) even that there
* are 6 hours difference between the two times. This difference can be
* preserved by using <code>setCellValue(value.getTime())</code> which will
* automatically shift the times to the default timezone.
*
* @param value the date value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For othertypes we
* will change the cell to a numeric cell and set its value.
*/
public void setCellValue(Calendar value) { public void setCellValue(Calendar value) {
// TODO Auto-generated method stub boolean date1904 = this.row.getSheet().getWorkbook().isDate1904();
setCellValue( DateUtil.getExcelDate(value, date1904 ));
} }
public void setCellValue(String str) { public void setCellValue(String str) {
@ -414,8 +455,42 @@ public final class XSSFCell implements Cell {
this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING); this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING);
} }
/**
* Returns a string representation of the cell
*
* This method returns a simple representation,
* anthing more complex should be in user code, with
* knowledge of the semantics of the sheet being processed.
*
* Formula cells return the formula string,
* rather than the formula result.
* Dates are displayed in dd-MMM-yyyy format
* Errors are displayed as #ERR&lt;errIdx&gt;
*/
public String toString() { public String toString() {
return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV(); // return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
switch (getCellType()) {
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_ERROR:
return ErrorEval.getText(getErrorCellValue());
case CELL_TYPE_FORMULA:
return getCellFormula();
case CELL_TYPE_NUMERIC:
//TODO apply the dataformat for this cell
if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
return sdf.format(getDateCellValue());
} else {
return getNumericCellValue() + "";
}
case CELL_TYPE_STRING:
return getRichStringCellValue().toString();
default:
return "Unknown Cell Type: " + getCellType();
}
} }
/** /**
@ -429,16 +504,15 @@ public final class XSSFCell implements Cell {
* @throws RuntimeException if the bounds are exceeded. * @throws RuntimeException if the bounds are exceeded.
*/ */
private void checkBounds(int cellNum) { private void checkBounds(int cellNum) {
if (cellNum > 255) { if (cellNum > 255) {
throw new RuntimeException("You cannot have more than 255 columns "+ throw new RuntimeException("You cannot have more than 255 columns " +
"in a given row (IV). Because Excel can't handle it"); "in a given row (IV). Because Excel can't handle it");
} } else if (cellNum < 0) {
else if (cellNum < 0) { throw new RuntimeException("You cannot reference columns with an index of less then 0.");
throw new RuntimeException("You cannot reference columns with an index of less then 0."); }
}
} }
public Hyperlink getHyperlink() { public Hyperlink getHyperlink() {
return row.getSheet().getHyperlink(row.getRowNum(), cellNum); return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
} }
public void setHyperlink(Hyperlink hyperlink) { public void setHyperlink(Hyperlink hyperlink) {

View File

@ -32,336 +32,531 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontPr
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
/**
* Represents a font used in a workbook.
*
* @author Gisella Bronzetti
*/
public class XSSFFont implements Font { public class XSSFFont implements Font {
public static final String DEFAULT_FONT_NAME="Calibri"; /**
public static final short DEFAULT_FONT_SIZE=11; * By default, Microsoft Office Excel 2007 uses the Calibry font in font size 11
*/
public static final String DEFAULT_FONT_NAME = "Calibri";
/**
* By default, Microsoft Office Excel 2007 uses the Calibry font in font size 11
*/
public static final short DEFAULT_FONT_SIZE = 11;
/**
* Default font color is black
* @see IndexedColors.BLACK
*/
public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex(); public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex();
private CTFont ctFont; private CTFont ctFont;
/**
* Create a new XSSFFont
*
* @param font the underlying CTFont bean
*/
public XSSFFont(CTFont font) { public XSSFFont(CTFont font) {
this.ctFont=font; this.ctFont = font;
} }
protected XSSFFont() { /**
this.ctFont = CTFont.Factory.newInstance(); * Create a new XSSFont. This method is protected to be used only by XSSFWorkbook
*/
protected XSSFFont() {
this.ctFont = CTFont.Factory.newInstance();
} }
/**
public CTFont getCTFont(){ * get the underlying CTFont font
return ctFont; */
public CTFont getCTFont() {
return ctFont;
} }
/**
public boolean getBold() { * get a boolean value for the boldness to use.
CTBooleanProperty bold=ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0); *
return (bold!=null && bold.getVal()); * @return boolean - bold
*/
public boolean getBold() {
CTBooleanProperty bold = ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0);
return (bold != null && bold.getVal());
} }
/**
* get character-set to use.
*
* @return byte - character-set
* @see FontCharset
*/
public byte getCharSet() { public byte getCharSet() {
CTIntProperty charset= ctFont.sizeOfCharsetArray() == 0?null:ctFont.getCharsetArray(0); CTIntProperty charset = ctFont.sizeOfCharsetArray() == 0 ? null : ctFont.getCharsetArray(0);
return charset == null ? FontCharset.ANSI.getValue() : FontCharset.valueOf(charset.getVal()).getValue(); return charset == null ? FontCharset.ANSI.getValue() : FontCharset.valueOf(charset.getVal()).getValue();
} }
/**
* get the indexed color value for the font
* References a color defined in IndexedColors.
*
* @return short - indexed color to use
* @see IndexedColors
*/
public short getColor() { public short getColor() {
CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); CTColor color = ctFont.sizeOfColorArray() == 0 ? null : ctFont.getColorArray(0);
if(color == null) return IndexedColors.BLACK.getIndex(); if (color == null) return IndexedColors.BLACK.getIndex();
long index=color.getIndexed(); long index = color.getIndexed();
if (index==XSSFFont.DEFAULT_FONT_COLOR){ if (index == XSSFFont.DEFAULT_FONT_COLOR) {
return IndexedColors.BLACK.getIndex(); return IndexedColors.BLACK.getIndex();
} } else if (index == IndexedColors.RED.getIndex()) {
else if(index == IndexedColors.RED.getIndex()){
return IndexedColors.RED.getIndex(); return IndexedColors.RED.getIndex();
} } else {
else{ return (short)index;
return Short.parseShort(new Long(index).toString());
} }
} }
public XSSFColor getRgbColor() { /**
CTColor ctColor=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); * get the color value for the font
XSSFColor color=new XSSFColor(ctColor); * References a color defined as Standard Alpha Red Green Blue color value (ARGB).
return color; *
} * @return XSSFColor - rgb color to use
*/
public short getThemeColor() { public XSSFColor getRgbColor() {
CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? null : ctFont.getColorArray(0);
long index=color.getTheme(); XSSFColor color = new XSSFColor(ctColor);
return (short)index; return color;
} }
/**
* get the color value for the font
* References a color defined in theme.
*
* @return short - theme defined to use
*/
public short getThemeColor() {
CTColor color = ctFont.sizeOfColorArray() == 0 ? null : ctFont.getColorArray(0);
long index = color == null ? 0 : color.getTheme();
return (short) index;
}
/**
* get the font height in point.
*
* @return short - height in point
*/
public short getFontHeight() { public short getFontHeight() {
CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0);
if(size!=null){ if (size != null) {
double fontHeight= size.getVal(); double fontHeight = size.getVal();
return (short)fontHeight; return (short) fontHeight;
} } else
else
return DEFAULT_FONT_SIZE; return DEFAULT_FONT_SIZE;
} }
/**
* @see #getFontHeight()
*/
public short getFontHeightInPoints() { public short getFontHeightInPoints() {
CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0);
if(size!=null){ if (size != null) {
double fontHeight= size.getVal(); double fontHeight = size.getVal();
return (short)fontHeight; return (short) fontHeight;
} } else
else
return DEFAULT_FONT_SIZE; return DEFAULT_FONT_SIZE;
} }
/**
* get the name of the font (i.e. Arial)
*
* @return String - a string representing the name of the font to use
*/
public String getFontName() { public String getFontName() {
CTFontName name = ctFont.sizeOfNameArray() == 0 ? null : ctFont.getNameArray(0); CTFontName name = ctFont.sizeOfNameArray() == 0 ? null : ctFont.getNameArray(0);
return name == null ? null : name.getVal(); return name == null ? null : name.getVal();
} }
/**
* get a boolean value that specify whether to use italics or not
*
* @return boolean - value for italic
*/
public boolean getItalic() {
CTBooleanProperty italic = ctFont.sizeOfIArray() == 0 ? null : ctFont.getIArray(0);
return italic != null && italic.getVal();
}
public boolean getItalic() { /**
CTBooleanProperty italic=ctFont.sizeOfIArray()==0?null:ctFont.getIArray(0); * get a boolean value that specify whether to use a strikeout horizontal line through the text or not
return italic!=null && italic.getVal(); *
} * @return boolean - value for strikeout
*/
public boolean getStrikeout() { public boolean getStrikeout() {
CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?null:ctFont.getStrikeArray(0); CTBooleanProperty strike = ctFont.sizeOfStrikeArray() == 0 ? null : ctFont.getStrikeArray(0);
return strike!=null && strike.getVal(); return strike != null && strike.getVal();
} }
/**
* get normal,super or subscript.
*
* @return short - offset type to use (none,super,sub)
* @see Font#SS_NONE
* @see Font#SS_SUPER
* @see Font#SS_SUB
*/
public short getTypeOffset() { public short getTypeOffset() {
CTVerticalAlignFontProperty vAlign=ctFont.sizeOfVertAlignArray()==0?null:ctFont.getVertAlignArray(0); CTVerticalAlignFontProperty vAlign = ctFont.sizeOfVertAlignArray() == 0 ? null : ctFont.getVertAlignArray(0);
if(vAlign!=null){ if (vAlign != null) {
int val=vAlign.getVal().intValue(); int val = vAlign.getVal().intValue();
switch (val) { switch (val) {
case STVerticalAlignRun.INT_BASELINE: case STVerticalAlignRun.INT_BASELINE:
return Font.SS_NONE; return Font.SS_NONE;
case STVerticalAlignRun.INT_SUBSCRIPT: case STVerticalAlignRun.INT_SUBSCRIPT:
return Font.SS_SUB; return Font.SS_SUB;
case STVerticalAlignRun.INT_SUPERSCRIPT: case STVerticalAlignRun.INT_SUPERSCRIPT:
return Font.SS_SUPER; return Font.SS_SUPER;
default: throw new RuntimeException("Wrong offset value "+val); default:
throw new RuntimeException("Wrong offset value " + val);
} }
} } else
else
return Font.SS_NONE; return Font.SS_NONE;
} }
public byte getUnderline() { /**
/* * get type of text underlining to use
CTUnderlineProperty underline = ctFont.sizeOfUArray() == 0 ? null : ctFont.getUArray(0); *
return underline == null ? (byte)FontUnderline.NONE.getValue().intValue() : (byte)FontUnderline.valueOf(underline.getVal()).getValue().intValue(); * @return byte - underlining type
*/ * @see FontUnderline
CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0); */
if(underline!=null){ public byte getUnderline() {
FontUnderline fontUnderline=FontUnderline.valueOf(underline.getVal()); CTUnderlineProperty underline = ctFont.sizeOfUArray() == 0 ? null : ctFont.getUArray(0);
switch (fontUnderline.getValue().intValue()) { if (underline != null) {
case STUnderlineValues.INT_DOUBLE: FontUnderline fontUnderline = FontUnderline.valueOf(underline.getVal());
return Font.U_DOUBLE; switch (fontUnderline.getValue().intValue()) {
case STUnderlineValues.INT_DOUBLE_ACCOUNTING: case STUnderlineValues.INT_DOUBLE:
return Font.U_DOUBLE_ACCOUNTING; return Font.U_DOUBLE;
case STUnderlineValues.INT_DOUBLE_ACCOUNTING:
return Font.U_DOUBLE_ACCOUNTING;
case STUnderlineValues.INT_SINGLE_ACCOUNTING: case STUnderlineValues.INT_SINGLE_ACCOUNTING:
return Font.U_SINGLE_ACCOUNTING; return Font.U_SINGLE_ACCOUNTING;
case STUnderlineValues.INT_NONE: case STUnderlineValues.INT_NONE:
return Font.U_NONE; return Font.U_NONE;
case STUnderlineValues.INT_SINGLE: case STUnderlineValues.INT_SINGLE:
default: default:
return Font.U_SINGLE; return Font.U_SINGLE;
} }
} }
return Font.U_NONE; return Font.U_NONE;
} }
/** /**
* Set characters in bold face font style. * set a boolean value for the boldness to use. If omitted, the default value is true.
* If omitted, the default value is true. *
*/ * @param bold - boldness to use
public void setBold(boolean bold) { */
CTBooleanProperty ctBold=ctFont.sizeOfBArray()==0?ctFont.addNewB():ctFont.getBArray(0); public void setBold(boolean bold) {
ctBold.setVal(true); CTBooleanProperty ctBold = ctFont.sizeOfBArray() == 0 ? ctFont.addNewB() : ctFont.getBArray(0);
} ctBold.setVal(true);
}
/**
* set character-set to use.
*
* @param charset - charset
* @see FontCharset
*/
public void setCharSet(byte charset) { public void setCharSet(byte charset) {
CTIntProperty charsetProperty=ctFont.sizeOfCharsetArray()==0?ctFont.addNewCharset():ctFont.getCharsetArray(0); CTIntProperty charsetProperty = ctFont.sizeOfCharsetArray() == 0 ? ctFont.addNewCharset() : ctFont.getCharsetArray(0);
switch (charset) { switch (charset) {
case Font.ANSI_CHARSET: case Font.ANSI_CHARSET:
charsetProperty.setVal(FontCharset.ANSI.getValue()); charsetProperty.setVal(FontCharset.ANSI.getValue());
break; break;
case Font.SYMBOL_CHARSET: case Font.SYMBOL_CHARSET:
charsetProperty.setVal(FontCharset.SYMBOL.getValue()); charsetProperty.setVal(FontCharset.SYMBOL.getValue());
break; break;
case Font.DEFAULT_CHARSET: case Font.DEFAULT_CHARSET:
charsetProperty.setVal(FontCharset.DEFAULT.getValue()); charsetProperty.setVal(FontCharset.DEFAULT.getValue());
break; break;
default: default:
throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset"); throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset");
} }
} }
/**
* set character-set to use.
*
* @param charSet
*/
public void setCharSet(FontCharset charSet) { public void setCharSet(FontCharset charSet) {
setCharSet(charSet.getValue()); setCharSet(charSet.getValue());
} }
/**
* set the indexed color for the font
*
* @param color - color to use
* @see #DEFAULT_FONT_COLOR - Note: default font color
* @see IndexedColors
*/
public void setColor(short color) { public void setColor(short color) {
CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? ctFont.addNewColor() : ctFont.getColorArray(0);
switch (color) { switch (color) {
case Font.COLOR_NORMAL:{ case Font.COLOR_NORMAL: {
ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
break; break;
} }
case Font.COLOR_RED:{ case Font.COLOR_RED: {
ctColor.setIndexed(IndexedColors.RED.getIndex()); ctColor.setIndexed(IndexedColors.RED.getIndex());
break; break;
} }
default: default:
ctColor.setIndexed(color); ctColor.setIndexed(color);
} }
} }
/**
* set the font height in points.
*
* @param height - height in points
*/
public void setFontHeight(short height) { public void setFontHeight(short height) {
setFontHeight((double)height); setFontHeight((double) height);
} }
/**
* set the font height in points.
*
* @param height - height in points
*/
public void setFontHeight(double height) { public void setFontHeight(double height) {
CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); CTFontSize fontSize = ctFont.sizeOfSzArray() == 0 ? ctFont.addNewSz() : ctFont.getSzArray(0);
fontSize.setVal(height); fontSize.setVal(height);
} }
/**
* set the font height in points.
*
* @link #setFontHeight
*/
public void setFontHeightInPoints(short height) { public void setFontHeightInPoints(short height) {
setFontHeight((double)height); setFontHeight(height);
} }
/**
* set the color for the font in Standard Alpha Red Green Blue color value
*
* @param color - color to use
*/
public void setRgbColor(XSSFColor color) { public void setRgbColor(XSSFColor color) {
CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? ctFont.addNewColor() : ctFont.getColorArray(0);
ctColor.setRgb(color.getRgb()); ctColor.setRgb(color.getRgb());
} }
public void setThemeColor(short theme) { /**
CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); * set the theme color for the font to use
ctColor.setTheme(theme); *
} * @param theme - theme color to use
*/
public void setThemeColor(short theme) {
CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? ctFont.addNewColor() : ctFont.getColorArray(0);
ctColor.setTheme(theme);
}
/**
* set the name for the font (i.e. Arial).
* If the font doesn't exist (because it isn't installed on the system),
* or the charset is invalid for that font, then another font should
* be substituted.
* The string length for this attribute shall be 0 to 31 characters.
* Default font name is Calibri.
*
* @param name - value representing the name of the font to use
* @see #DEFAULT_FONT_NAME
*/
public void setFontName(String name) { public void setFontName(String name) {
CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0); CTFontName fontName = ctFont.sizeOfNameArray() == 0 ? ctFont.addNewName() : ctFont.getNameArray(0);
fontName.setVal(name); fontName.setVal(name);
} }
/**
* set a boolean value for the property specifying whether to use italics or not
* If omitted, the default value is true.
*
* @param italic - value for italics or not
*/
public void setItalic(boolean italic) { public void setItalic(boolean italic) {
CTBooleanProperty bool=ctFont.sizeOfIArray()==0?ctFont.addNewI():ctFont.getIArray(0); CTBooleanProperty bool = ctFont.sizeOfIArray() == 0 ? ctFont.addNewI() : ctFont.getIArray(0);
bool.setVal(italic); bool.setVal(italic);
} }
/**
* set a boolean value for the property specifying whether to use a strikeout horizontal line through the text or not
* If omitted, the default value is true.
*
* @param strikeout - value for strikeout or not
*/
public void setStrikeout(boolean strikeout) { public void setStrikeout(boolean strikeout) {
CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?ctFont.addNewStrike():ctFont.getStrikeArray(0); CTBooleanProperty strike = ctFont.sizeOfStrikeArray() == 0 ? ctFont.addNewStrike() : ctFont.getStrikeArray(0);
strike.setVal(strikeout); strike.setVal(strikeout);
} }
/**
* set normal,super or subscript, that representing the vertical-alignment setting.
* Setting this to either subscript or superscript shall make the font size smaller if a
* smaller font size is available.
*
* @param offset - offset type to use (none,super,sub)
* @see #SS_NONE
* @see #SS_SUPER
* @see #SS_SUB
*/
public void setTypeOffset(short offset) { public void setTypeOffset(short offset) {
CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0); CTVerticalAlignFontProperty offsetProperty = ctFont.sizeOfVertAlignArray() == 0 ? ctFont.addNewVertAlign() : ctFont.getVertAlignArray(0);
switch (offset) { switch (offset) {
case Font.SS_NONE: case Font.SS_NONE:
offsetProperty.setVal(STVerticalAlignRun.BASELINE); offsetProperty.setVal(STVerticalAlignRun.BASELINE);
break; break;
case Font.SS_SUB: case Font.SS_SUB:
offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT); offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);
break; break;
case Font.SS_SUPER: case Font.SS_SUPER:
offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT); offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);
break; break;
} }
} }
/**
* set the style of underlining that is used.
* The none style is equivalent to not using underlining at all.
*
* @param underline - underline type to use
* @see FontUnderline
*/
public void setUnderline(byte underline) { public void setUnderline(byte underline) {
CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0); CTUnderlineProperty ctUnderline = ctFont.sizeOfUArray() == 0 ? ctFont.addNewU() : ctFont.getUArray(0);
switch (underline) { switch (underline) {
case Font.U_DOUBLE: case Font.U_DOUBLE:
ctUnderline.setVal(FontUnderline.DOUBLE.getValue()); ctUnderline.setVal(FontUnderline.DOUBLE.getValue());
break; break;
case Font.U_DOUBLE_ACCOUNTING: case Font.U_DOUBLE_ACCOUNTING:
ctUnderline.setVal(FontUnderline.DOUBLE_ACCOUNTING.getValue()); ctUnderline.setVal(FontUnderline.DOUBLE_ACCOUNTING.getValue());
break; break;
case Font.U_SINGLE_ACCOUNTING: case Font.U_SINGLE_ACCOUNTING:
ctUnderline.setVal(FontUnderline.SINGLE_ACCOUNTING.getValue()); ctUnderline.setVal(FontUnderline.SINGLE_ACCOUNTING.getValue());
break; break;
case Font.U_NONE: case Font.U_NONE:
ctUnderline.setVal(FontUnderline.NONE.getValue()); ctUnderline.setVal(FontUnderline.NONE.getValue());
break; break;
case Font.U_SINGLE: case Font.U_SINGLE:
default: default:
ctUnderline.setVal(FontUnderline.SINGLE.getValue()); ctUnderline.setVal(FontUnderline.SINGLE.getValue());
break; break;
} }
} }
/**
* set an enumeration representing the style of underlining that is used.
* The none style is equivalent to not using underlining at all.
* The possible values for this attribute are defined by the FontUnderline
*
* @param underline - FontUnderline enum value
*/
public void setUnderline(FontUnderline underline) { public void setUnderline(FontUnderline underline) {
CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0); CTUnderlineProperty ctUnderline = ctFont.sizeOfUArray() == 0 ? ctFont.addNewU() : ctFont.getUArray(0);
ctUnderline.setVal(underline.getValue()); ctUnderline.setVal(underline.getValue());
} }
public String toString(){
return "org.apache.poi.xssf.usermodel.XSSFFont{" +
ctFont +
"}";
}
public long putFont(ArrayList<CTFont> fonts) {
public String toString() {
return "org.apache.poi.xssf.usermodel.XSSFFont{" +
ctFont +
"}";
}
public long putFont(ArrayList<CTFont> fonts) {
//TODO //TODO
/* /*
* we need to implement a method equals to check that 2 instances of CTFont * we need to implement a method equals to check that 2 instances of CTFont
* are different by comparison of all font attributes. * are different by comparison of all font attributes.
* NB: take a look to findFont method in XSSFWorkbook * NB: take a look to findFont method in XSSFWorkbook
*/ */
if(fonts.contains(ctFont)) { if (fonts.contains(ctFont)) {
return fonts.indexOf(ctFont); return fonts.indexOf(ctFont);
} }
fonts.add(ctFont); fonts.add(ctFont);
return fonts.size() - 1; return fonts.size() - 1;
} }
/* /**
this methds are used only for XSSFFont and aren't in Font interface * get the font scheme property.
are used in method SthlesTable.createDefaultfont * is used only in StylesTable to create the default instance of font
*/ *
* @return FontScheme
public FontScheme getScheme(){ * @see org.apache.poi.xssf.model.StylesTable#createDefaultFont()
CTFontScheme scheme=ctFont.sizeOfSchemeArray()==0?null:ctFont.getSchemeArray(0); */
public FontScheme getScheme() {
CTFontScheme scheme = ctFont.sizeOfSchemeArray() == 0 ? null : ctFont.getSchemeArray(0);
return scheme == null ? FontScheme.NONE : FontScheme.valueOf(scheme.getVal()); return scheme == null ? FontScheme.NONE : FontScheme.valueOf(scheme.getVal());
} }
public void setScheme(FontScheme scheme){ /**
CTFontScheme ctFontScheme=ctFont.sizeOfSchemeArray()==0?ctFont.addNewScheme():ctFont.getSchemeArray(0); * set font scheme property
ctFontScheme.setVal(scheme.getValue()); *
* @param scheme - FontScheme enum value
* @see FontScheme
*/
public void setScheme(FontScheme scheme) {
CTFontScheme ctFontScheme = ctFont.sizeOfSchemeArray() == 0 ? ctFont.addNewScheme() : ctFont.getSchemeArray(0);
ctFontScheme.setVal(scheme.getValue());
}
/**
* get the font family to use.
*
* @return the font family to use
* @see FontFamily
*/
public int getFamily() {
CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0);
return family == null ? FontFamily.NOT_APPLICABLE.getValue() : FontFamily.valueOf(family.getVal()).getValue();
}
/**
* Set the font family this font belongs to.
* A font family is a set of fonts having common stroke width and serif characteristics.
* The font name overrides when there are conflicting values.
*
* @param value - font family
* @see FontFamily
*/
public void setFamily(int value) {
CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0);
family.setVal(value);
}
/**
* set an enumeration representing the font family this font belongs to.
* A font family is a set of fonts having common stroke width and serif characteristics.
*
* @param family font family
* @link #setFamily(int value)
*/
public void setFamily(FontFamily family) {
setFamily(family.getValue());
} }
public int getFamily(){
CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0);
return family == null ? FontFamily.NOT_APPLICABLE.getValue() : FontFamily.valueOf(family.getVal()).getValue();
}
public void setFamily(int value){
CTIntProperty family = ctFont.sizeOfFamilyArray() == 0 ? ctFont.addNewFamily() : ctFont.getFamilyArray(0);
family.setVal(value);
}
public void setFamily(FontFamily family){
setFamily(family.getValue());
}
} }

View File

@ -438,9 +438,28 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return false; return false;
} }
/**
* Get whether to display the guts or not,
* default value is true
*
* @return boolean - guts or no guts
*/
public boolean getDisplayGuts() { public boolean getDisplayGuts() {
// TODO Auto-generated method stub CTSheetPr sheetPr = getSheetTypeSheetPr();
return false; CTOutlinePr outlinePr = sheetPr.getOutlinePr() == null ? CTOutlinePr.Factory.newInstance() : sheetPr.getOutlinePr();
return outlinePr.getShowOutlineSymbols();
}
/**
* Set whether to display the guts or not
*
* @param value - guts or no guts
*/
public void setDisplayGuts(boolean value) {
CTSheetPr sheetPr = getSheetTypeSheetPr();
CTOutlinePr outlinePr = sheetPr.getOutlinePr() == null ? sheetPr.addNewOutlinePr() : sheetPr.getOutlinePr();
outlinePr.setShowOutlineSymbols(value);
} }
/** /**
@ -1117,11 +1136,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
getSheetTypeSheetView().setShowGridLines(show); getSheetTypeSheetView().setShowGridLines(show);
} }
public void setDisplayGuts(boolean b) {
// TODO Auto-generated method stub
}
public void setDisplayRowColHeadings(boolean show) { public void setDisplayRowColHeadings(boolean show) {
getSheetTypeSheetView().setShowRowColHeaders(show); getSheetTypeSheetView().setShowRowColHeaders(show);
} }

View File

@ -23,17 +23,21 @@ import java.util.Date;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.SharedStringSource; import org.apache.poi.xssf.model.SharedStringSource;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@ -108,6 +112,10 @@ public final class TestXSSFCell extends TestCase {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// success // success
} }
cell.setCellValue(cal);
assertEquals(before1904,cell.getDateCellValue());
} }
public void testSetGetError() throws Exception { public void testSetGetError() throws Exception {
@ -379,4 +387,82 @@ public final class TestXSSFCell extends TestCase {
fail(); fail();
} catch(IllegalArgumentException e) {} } catch(IllegalArgumentException e) {}
} }
public void testHSSFXSSFToString(){
Workbook xwb = new XSSFWorkbook();
Sheet xsheet = xwb.createSheet();
XSSFCell xcell = (XSSFCell) xsheet.createRow(0).createCell((short)0);
Workbook hwb=new HSSFWorkbook();
Sheet hsheet=hwb.createSheet();
HSSFCell hcell = (HSSFCell) hsheet.createRow(0).createCell((short)0);
//BLANK
assertEquals(hcell.toString(),xcell.toString());
System.out.println("BLANK==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
//BOOLEAN
xcell.setCellValue(true);
xcell.setCellType(Cell.CELL_TYPE_BOOLEAN);
hcell.setCellValue(true);
hcell.setCellType(Cell.CELL_TYPE_BOOLEAN);
System.out.println("BOOLEAN==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
//NUMERIC
xcell.setCellValue(1234);
xcell.setCellType(Cell.CELL_TYPE_NUMERIC);
hcell.setCellValue(1234);
hcell.setCellType(Cell.CELL_TYPE_NUMERIC);
System.out.println("NUMERIC==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
//DATE ********************
Calendar cal = Calendar.getInstance();
cal.set(1903, 1, 8);
xcell.setCellValue(cal.getTime());
CellStyle xstyle=xwb.createCellStyle();
DataFormat format = xwb.createDataFormat();
xstyle.setDataFormat(format.getFormat("YYYY-MM-DD"));
xcell.setCellStyle(xstyle);
hcell.setCellValue(cal.getTime());
CellStyle hstyle=hwb.createCellStyle();
DataFormat hformat = hwb.createDataFormat();
hstyle.setDataFormat(hformat.getFormat("YYYY-MM-DD"));
hcell.setCellStyle(hstyle);
System.out.println("DATE==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
//STRING
xcell.setCellValue(new XSSFRichTextString("text string"));
xcell.setCellType(Cell.CELL_TYPE_STRING);
hcell.setCellValue(new HSSFRichTextString("text string"));
hcell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("STRING==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
//ERROR
xcell.setCellErrorValue(Cell.ERROR_VALUE);
xcell.setCellType(Cell.CELL_TYPE_ERROR);
hcell.setCellErrorValue((byte)0);
hcell.setCellType(Cell.CELL_TYPE_ERROR);
System.out.println("ERROR==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
//FORMULA
xcell.setCellFormula("A1+B2");
hcell.setCellValue("A1+B2");
System.out.println("FORMULA==> xssf="+xcell.toString() + " - hssf="+hcell.toString());
assertEquals(hcell.toString(),xcell.toString());
}
} }

View File

@ -510,6 +510,14 @@ public class TestXSSFSheet extends TestCase {
assertFalse(sheet.isDisplayGridlines()); assertFalse(sheet.isDisplayGridlines());
} }
public void testIsSetDisplayGuts() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
assertTrue(sheet.getDisplayGuts());
sheet.setDisplayGuts(false);
assertFalse(sheet.getDisplayGuts());
}
public void testIsSetDisplayRowColHeadings() { public void testIsSetDisplayRowColHeadings() {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");