whitespace (tabs to 4 spaces)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1737895 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-04-05 23:47:41 +00:00
parent fe13debb84
commit c95f6a4b35
3 changed files with 512 additions and 512 deletions

View File

@ -46,7 +46,7 @@ public final class HSSFCellStyle implements CellStyle {
/** Creates new HSSFCellStyle why would you want to do this?? */
protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook)
{
this(index, rec, workbook.getWorkbook());
this(index, rec, workbook.getWorkbook());
}
protected HSSFCellStyle(short index, ExtendedFormatRecord rec, InternalWorkbook workbook)
{
@ -74,14 +74,14 @@ public final class HSSFCellStyle implements CellStyle {
public HSSFCellStyle getParentStyle() {
short parentIndex = _format.getParentIndex();
// parentIndex equal 0xFFF indicates no inheritance from a cell style XF (See 2.4.353 XF)
if(parentIndex == 0 || parentIndex == 0xFFF) {
return null;
}
return new HSSFCellStyle(
parentIndex,
_workbook.getExFormatAt(parentIndex),
_workbook
);
if(parentIndex == 0 || parentIndex == 0xFFF) {
return null;
}
return new HSSFCellStyle(
parentIndex,
_workbook.getExFormatAt(parentIndex),
_workbook
);
}
/**
@ -145,7 +145,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return the format string or "General" if not found
*/
public String getDataFormatString(org.apache.poi.ss.usermodel.Workbook workbook) {
HSSFDataFormat format = new HSSFDataFormat( ((HSSFWorkbook)workbook).getWorkbook() );
HSSFDataFormat format = new HSSFDataFormat( ((HSSFWorkbook)workbook).getWorkbook() );
int idx = getDataFormat();
return idx == -1 ? "General" : format.getFormat(getDataFormat());
@ -156,7 +156,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see org.apache.poi.hssf.usermodel.HSSFDataFormat
*/
public String getDataFormatString(org.apache.poi.hssf.model.InternalWorkbook workbook) {
HSSFDataFormat format = new HSSFDataFormat( workbook );
HSSFDataFormat format = new HSSFDataFormat( workbook );
return format.getFormat(getDataFormat());
}
@ -169,9 +169,9 @@ public final class HSSFCellStyle implements CellStyle {
*/
@Override
public void setFont(Font font) {
setFont((HSSFFont)font);
}
public void setFont(HSSFFont font) {
setFont((HSSFFont)font);
}
public void setFont(HSSFFont font) {
_format.setIndentNotParentFont(true);
short fontindex = font.getIndex();
_format.setFontIndex(fontindex);
@ -194,7 +194,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/
public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) {
return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex());
return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex());
}
/**
@ -714,15 +714,15 @@ public final class HSSFCellStyle implements CellStyle {
*/
private void checkDefaultBackgroundFills() {
if (_format.getFillForeground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) {
//JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt
//to me now.... But experience has shown that when the fore is set to AUTOMATIC then the
//background needs to be incremented......
if (_format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1))
setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1));
//JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt
//to me now.... But experience has shown that when the fore is set to AUTOMATIC then the
//background needs to be incremented......
if (_format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1))
setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1));
} else if (_format.getFillBackground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)
//Now if the forground changes to a non-AUTOMATIC color the background resets itself!!!
if (_format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index)
setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index);
//Now if the forground changes to a non-AUTOMATIC color the background resets itself!!!
if (_format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index)
setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index);
}
/**
@ -767,13 +767,13 @@ public final class HSSFCellStyle implements CellStyle {
@Override
public short getFillBackgroundColor()
{
short result = _format.getFillBackground();
//JMH: Do this ridiculous conversion, and let HSSFCellStyle
//internally migrate back and forth
if (result == (HSSFColor.AUTOMATIC.index+1)) {
return HSSFColor.AUTOMATIC.index;
}
return result;
short result = _format.getFillBackground();
//JMH: Do this ridiculous conversion, and let HSSFCellStyle
//internally migrate back and forth
if (result == (HSSFColor.AUTOMATIC.index+1)) {
return HSSFColor.AUTOMATIC.index;
}
return result;
}
@Override
@ -827,14 +827,14 @@ public final class HSSFCellStyle implements CellStyle {
* styles where no name has been defined
*/
public String getUserStyleName() {
StyleRecord sr = _workbook.getStyleRecord(_index);
if(sr == null) {
return null;
}
if(sr.isBuiltin()) {
return null;
}
return sr.getName();
StyleRecord sr = _workbook.getStyleRecord(_index);
if(sr == null) {
return null;
}
if(sr.isBuiltin()) {
return null;
}
return sr.getName();
}
/**
@ -842,16 +842,16 @@ public final class HSSFCellStyle implements CellStyle {
* Will complain if you try this on a built in style.
*/
public void setUserStyleName(String styleName) {
StyleRecord sr = _workbook.getStyleRecord(_index);
if(sr == null) {
sr = _workbook.createStyleRecord(_index);
}
// All Style records start as "builtin", but generally
// only 20 and below really need to be
if(sr.isBuiltin() && _index <= 20) {
throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
}
sr.setName(styleName);
StyleRecord sr = _workbook.getStyleRecord(_index);
if(sr == null) {
sr = _workbook.createStyleRecord(_index);
}
// All Style records start as "builtin", but generally
// only 20 and below really need to be
if(sr.isBuiltin() && _index <= 20) {
throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
}
sr.setName(styleName);
}
/**
@ -860,7 +860,7 @@ public final class HSSFCellStyle implements CellStyle {
*/
@Override
public void setShrinkToFit(boolean shrinkToFit) {
_format.setShrinkToFit(shrinkToFit);
_format.setShrinkToFit(shrinkToFit);
}
/**
* Should the Cell be auto-sized by Excel to shrink
@ -868,9 +868,9 @@ public final class HSSFCellStyle implements CellStyle {
*/
@Override
public boolean getShrinkToFit() {
return _format.getShrinkToFit();
return _format.getShrinkToFit();
}
/**
* Get the reading order, for RTL/LTR ordering of
* the text.
@ -903,10 +903,10 @@ public final class HSSFCellStyle implements CellStyle {
* @throws IllegalArgumentException if there's a workbook mis-match
*/
public void verifyBelongsToWorkbook(HSSFWorkbook wb) {
if(wb.getWorkbook() != _workbook) {
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
}
}
if(wb.getWorkbook() != _workbook) {
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
}
}
/**
* Clones all the style information from another
@ -922,71 +922,71 @@ public final class HSSFCellStyle implements CellStyle {
*/
@Override
public void cloneStyleFrom(CellStyle source) {
if(source instanceof HSSFCellStyle) {
this.cloneStyleFrom((HSSFCellStyle)source);
} else {
throw new IllegalArgumentException("Can only clone from one HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
}
}
if(source instanceof HSSFCellStyle) {
this.cloneStyleFrom((HSSFCellStyle)source);
} else {
throw new IllegalArgumentException("Can only clone from one HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
}
}
public void cloneStyleFrom(HSSFCellStyle source) {
// First we need to clone the extended format
// record
_format.cloneStyleFrom(source._format);
// First we need to clone the extended format
// record
_format.cloneStyleFrom(source._format);
// Handle matching things if we cross workbooks
if(_workbook != source._workbook) {
// Handle matching things if we cross workbooks
if(_workbook != source._workbook) {
lastDateFormat.set(Short.MIN_VALUE);
lastFormats.set(null);
getDataFormatStringCache.set(null);
// Then we need to clone the format string,
// and update the format record for this
short fmt = (short)_workbook.createFormat(source.getDataFormatString() );
setDataFormat(fmt);
// Then we need to clone the format string,
// and update the format record for this
short fmt = (short)_workbook.createFormat(source.getDataFormatString() );
setDataFormat(fmt);
// Finally we need to clone the font,
// and update the format record for this
FontRecord fr = _workbook.createNewFont();
fr.cloneStyleFrom(
source._workbook.getFontRecordAt(
source.getFontIndex()
)
);
// Finally we need to clone the font,
// and update the format record for this
FontRecord fr = _workbook.createNewFont();
fr.cloneStyleFrom(
source._workbook.getFontRecordAt(
source.getFontIndex()
)
);
HSSFFont font = new HSSFFont(
(short)_workbook.getFontIndex(fr), fr
);
setFont(font);
}
HSSFFont font = new HSSFFont(
(short)_workbook.getFontIndex(fr), fr
);
setFont(font);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_format == null) ? 0 : _format.hashCode());
result = prime * result + _index;
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_format == null) ? 0 : _format.hashCode());
result = prime * result + _index;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (obj instanceof HSSFCellStyle) {
final HSSFCellStyle other = (HSSFCellStyle) obj;
if (_format == null) {
if (other._format != null)
return false;
} else if (!_format.equals(other._format))
return false;
if (_index != other._index)
return false;
return true;
}
return false;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (obj instanceof HSSFCellStyle) {
final HSSFCellStyle other = (HSSFCellStyle) obj;
if (_format == null) {
if (other._format != null)
return false;
} else if (!_format.equals(other._format))
return false;
if (_index != other._index)
return false;
return true;
}
return false;
}
}

View File

@ -42,433 +42,433 @@ import org.apache.poi.ss.usermodel.Workbook;
*/
public final class CellUtil {
public static final String ALIGNMENT = "alignment";
public static final String BORDER_BOTTOM = "borderBottom";
public static final String BORDER_LEFT = "borderLeft";
public static final String BORDER_RIGHT = "borderRight";
public static final String BORDER_TOP = "borderTop";
public static final String BOTTOM_BORDER_COLOR = "bottomBorderColor";
public static final String DATA_FORMAT = "dataFormat";
public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor";
public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor";
public static final String FILL_PATTERN = "fillPattern";
public static final String FONT = "font";
public static final String HIDDEN = "hidden";
public static final String INDENTION = "indention";
public static final String LEFT_BORDER_COLOR = "leftBorderColor";
public static final String LOCKED = "locked";
public static final String RIGHT_BORDER_COLOR = "rightBorderColor";
public static final String ROTATION = "rotation";
public static final String TOP_BORDER_COLOR = "topBorderColor";
public static final String VERTICAL_ALIGNMENT = "verticalAlignment";
public static final String WRAP_TEXT = "wrapText";
public static final String ALIGNMENT = "alignment";
public static final String BORDER_BOTTOM = "borderBottom";
public static final String BORDER_LEFT = "borderLeft";
public static final String BORDER_RIGHT = "borderRight";
public static final String BORDER_TOP = "borderTop";
public static final String BOTTOM_BORDER_COLOR = "bottomBorderColor";
public static final String DATA_FORMAT = "dataFormat";
public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor";
public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor";
public static final String FILL_PATTERN = "fillPattern";
public static final String FONT = "font";
public static final String HIDDEN = "hidden";
public static final String INDENTION = "indention";
public static final String LEFT_BORDER_COLOR = "leftBorderColor";
public static final String LOCKED = "locked";
public static final String RIGHT_BORDER_COLOR = "rightBorderColor";
public static final String ROTATION = "rotation";
public static final String TOP_BORDER_COLOR = "topBorderColor";
public static final String VERTICAL_ALIGNMENT = "verticalAlignment";
public static final String WRAP_TEXT = "wrapText";
private static UnicodeMapping unicodeMappings[];
private static UnicodeMapping unicodeMappings[];
private static final class UnicodeMapping {
private static final class UnicodeMapping {
public final String entityName;
public final String resolvedValue;
public final String entityName;
public final String resolvedValue;
public UnicodeMapping(String pEntityName, String pResolvedValue) {
entityName = "&" + pEntityName + ";";
resolvedValue = pResolvedValue;
}
}
public UnicodeMapping(String pEntityName, String pResolvedValue) {
entityName = "&" + pEntityName + ";";
resolvedValue = pResolvedValue;
}
}
private CellUtil() {
// no instances of this class
}
private CellUtil() {
// no instances of this class
}
/**
* Get a row from the spreadsheet, and create it if it doesn't exist.
*
*@param rowIndex The 0 based row number
*@param sheet The sheet that the row is part of.
*@return The row indicated by the rowCounter
*/
public static Row getRow(int rowIndex, Sheet sheet) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
return row;
}
/**
* Get a row from the spreadsheet, and create it if it doesn't exist.
*
*@param rowIndex The 0 based row number
*@param sheet The sheet that the row is part of.
*@return The row indicated by the rowCounter
*/
public static Row getRow(int rowIndex, Sheet sheet) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
return row;
}
/**
* Get a specific cell from a row. If the cell doesn't exist, then create it.
*
*@param row The row that the cell is part of
*@param columnIndex The column index that the cell is in.
*@return The cell indicated by the column.
*/
public static Cell getCell(Row row, int columnIndex) {
Cell cell = row.getCell(columnIndex);
/**
* Get a specific cell from a row. If the cell doesn't exist, then create it.
*
*@param row The row that the cell is part of
*@param columnIndex The column index that the cell is in.
*@return The cell indicated by the column.
*/
public static Cell getCell(Row row, int columnIndex) {
Cell cell = row.getCell(columnIndex);
if (cell == null) {
cell = row.createCell(columnIndex);
}
return cell;
}
if (cell == null) {
cell = row.createCell(columnIndex);
}
return cell;
}
/**
* Creates a cell, gives it a value, and applies a style if provided
*
* @param row the row to create the cell in
* @param column the column index to create the cell in
* @param value The value of the cell
* @param style If the style is not null, then set
* @return A new Cell
*/
public static Cell createCell(Row row, int column, String value, CellStyle style) {
Cell cell = getCell(row, column);
/**
* Creates a cell, gives it a value, and applies a style if provided
*
* @param row the row to create the cell in
* @param column the column index to create the cell in
* @param value The value of the cell
* @param style If the style is not null, then set
* @return A new Cell
*/
public static Cell createCell(Row row, int column, String value, CellStyle style) {
Cell cell = getCell(row, column);
cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
.createRichTextString(value));
if (style != null) {
cell.setCellStyle(style);
}
return cell;
}
cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
.createRichTextString(value));
if (style != null) {
cell.setCellStyle(style);
}
return cell;
}
/**
* Create a cell, and give it a value.
*
*@param row the row to create the cell in
*@param column the column index to create the cell in
*@param value The value of the cell
*@return A new Cell.
*/
public static Cell createCell(Row row, int column, String value) {
return createCell(row, column, value, null);
}
/**
* Create a cell, and give it a value.
*
*@param row the row to create the cell in
*@param column the column index to create the cell in
*@param value The value of the cell
*@return A new Cell.
*/
public static Cell createCell(Row row, int column, String value) {
return createCell(row, column, value, null);
}
/**
* Take a cell, and align it.
*
*@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with.
*@param align the column alignment to use.
*
* @see CellStyle for alignment options
*/
public static void setAlignment(Cell cell, Workbook workbook, short align) {
setCellStyleProperty(cell, workbook, ALIGNMENT, Short.valueOf(align));
}
/**
* Take a cell, and align it.
*
*@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with.
*@param align the column alignment to use.
*
* @see CellStyle for alignment options
*/
public static void setAlignment(Cell cell, Workbook workbook, short align) {
setCellStyleProperty(cell, workbook, ALIGNMENT, Short.valueOf(align));
}
/**
* Take a cell, and apply a font to it
*
*@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with.
*@param font The Font that you want to set...
*/
public static void setFont(Cell cell, Workbook workbook, Font font) {
// Check if font belongs to workbook
final short fontIndex = font.getIndex();
if (!workbook.getFontAt(fontIndex).equals(font)) {
throw new IllegalArgumentException("Font does not belong to this workbook");
}
/**
* Take a cell, and apply a font to it
*
*@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with.
*@param font The Font that you want to set...
*/
public static void setFont(Cell cell, Workbook workbook, Font font) {
// Check if font belongs to workbook
final short fontIndex = font.getIndex();
if (!workbook.getFontAt(fontIndex).equals(font)) {
throw new IllegalArgumentException("Font does not belong to this workbook");
}
// Check if cell belongs to workbook
// (checked in setCellStyleProperty)
// Check if cell belongs to workbook
// (checked in setCellStyleProperty)
setCellStyleProperty(cell, workbook, FONT, fontIndex);
}
setCellStyleProperty(cell, workbook, FONT, fontIndex);
}
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus styles properties in <code>properties</code>. A new style is created if the
* workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>This is necessary because Excel has an upper limit on the number of styles that it supports.</p>
*
* <p>This function is more efficient than multiple calls to
* {@link #setCellStyleProperty(org.apache.poi.ss.usermodel.Cell, org.apache.poi.ss.usermodel.Workbook, String, Object)}
* if adding multiple cell styles.</p>
*
* <p>For performance reasons, if this is the only cell in a workbook that uses a cell style,
* this method does NOT remove the old style from the workbook.
* <!-- NOT IMPLEMENTED: Unused styles should be
* pruned from the workbook with [@link #removeUnusedCellStyles(Workbook)] or
* [@link #removeStyleFromWorkbookIfUnused(CellStyle, Workbook)]. -->
* </p>
*
* @param cell The cell to change the style of
* @param properties The properties to be added to a cell style, as {propertyName: propertyValue}.
* @since POI 3.14 beta 2
*/
public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) {
@SuppressWarnings("resource")
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus styles properties in <code>properties</code>. A new style is created if the
* workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>This is necessary because Excel has an upper limit on the number of styles that it supports.</p>
*
* <p>This function is more efficient than multiple calls to
* {@link #setCellStyleProperty(org.apache.poi.ss.usermodel.Cell, org.apache.poi.ss.usermodel.Workbook, String, Object)}
* if adding multiple cell styles.</p>
*
* <p>For performance reasons, if this is the only cell in a workbook that uses a cell style,
* this method does NOT remove the old style from the workbook.
* <!-- NOT IMPLEMENTED: Unused styles should be
* pruned from the workbook with [@link #removeUnusedCellStyles(Workbook)] or
* [@link #removeStyleFromWorkbookIfUnused(CellStyle, Workbook)]. -->
* </p>
*
* @param cell The cell to change the style of
* @param properties The properties to be added to a cell style, as {propertyName: propertyValue}.
* @since POI 3.14 beta 2
*/
public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) {
@SuppressWarnings("resource")
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle originalStyle = cell.getCellStyle();
CellStyle newStyle = null;
Map<String, Object> values = getFormatProperties(originalStyle);
values.putAll(properties);
CellStyle originalStyle = cell.getCellStyle();
CellStyle newStyle = null;
Map<String, Object> values = getFormatProperties(originalStyle);
values.putAll(properties);
// index seems like what index the cellstyle is in the list of styles for a workbook.
// not good to compare on!
int numberCellStyles = workbook.getNumCellStyles();
// index seems like what index the cellstyle is in the list of styles for a workbook.
// not good to compare on!
int numberCellStyles = workbook.getNumCellStyles();
for (int i = 0; i < numberCellStyles; i++) {
CellStyle wbStyle = workbook.getCellStyleAt(i);
Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);
for (int i = 0; i < numberCellStyles; i++) {
CellStyle wbStyle = workbook.getCellStyleAt(i);
Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);
// the desired style already exists in the workbook. Use the existing style.
if (wbStyleMap.equals(values)) {
newStyle = wbStyle;
break;
}
}
// the desired style already exists in the workbook. Use the existing style.
if (wbStyleMap.equals(values)) {
newStyle = wbStyle;
break;
}
}
// the desired style does not exist in the workbook. Create a new style with desired properties.
if (newStyle == null) {
newStyle = workbook.createCellStyle();
setFormatProperties(newStyle, workbook, values);
}
// the desired style does not exist in the workbook. Create a new style with desired properties.
if (newStyle == null) {
newStyle = workbook.createCellStyle();
setFormatProperties(newStyle, workbook, values);
}
cell.setCellStyle(newStyle);
}
cell.setCellStyle(newStyle);
}
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus a single style property <code>propertyName</code> with value
* <code>propertyValue<code>.
* A new style is created if the workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>If setting more than one cell style property on a cell, use
* {@link #setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)},
* which is faster and does not add unnecessary intermediate CellStyles to the workbook.</p>
*
* @param workbook The workbook that is being worked with.
* @param propertyName The name of the property that is to be changed.
* @param propertyValue The value of the property that is to be changed.
* @param cell The cell that needs it's style changes
*/
public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName,
Object propertyValue) {
if (cell.getSheet().getWorkbook() != workbook) {
throw new IllegalArgumentException("Cannot set cell style property. Cell does not belong to workbook.");
}
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus a single style property <code>propertyName</code> with value
* <code>propertyValue<code>.
* A new style is created if the workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>If setting more than one cell style property on a cell, use
* {@link #setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)},
* which is faster and does not add unnecessary intermediate CellStyles to the workbook.</p>
*
* @param workbook The workbook that is being worked with.
* @param propertyName The name of the property that is to be changed.
* @param propertyValue The value of the property that is to be changed.
* @param cell The cell that needs it's style changes
*/
public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName,
Object propertyValue) {
if (cell.getSheet().getWorkbook() != workbook) {
throw new IllegalArgumentException("Cannot set cell style property. Cell does not belong to workbook.");
}
Map<String, Object> values = Collections.singletonMap(propertyName, propertyValue);
setCellStyleProperties(cell, values);
}
Map<String, Object> values = Collections.singletonMap(propertyName, propertyValue);
setCellStyleProperties(cell, values);
}
/**
* Returns a map containing the format properties of the given cell style.
/**
* Returns a map containing the format properties of the given cell style.
* The returned map is not tied to <code>style</code>, so subsequent changes
* to <code>style</code> will not modify the map, and changes to the returned
* map will not modify the cell style. The returned map is mutable.
*
* @param style cell style
* @return map of format properties (String -> Object)
* @see #setFormatProperties(org.apache.poi.ss.usermodel.CellStyle, org.apache.poi.ss.usermodel.Workbook, java.util.Map)
*/
private static Map<String, Object> getFormatProperties(CellStyle style) {
Map<String, Object> properties = new HashMap<String, Object>();
putShort(properties, ALIGNMENT, style.getAlignment());
putBorderStyle(properties, BORDER_BOTTOM, style.getBorderBottom());
putBorderStyle(properties, BORDER_LEFT, style.getBorderLeft());
putBorderStyle(properties, BORDER_RIGHT, style.getBorderRight());
putBorderStyle(properties, BORDER_TOP, style.getBorderTop());
putShort(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor());
putShort(properties, DATA_FORMAT, style.getDataFormat());
putShort(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor());
putShort(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor());
putShort(properties, FILL_PATTERN, style.getFillPattern());
putShort(properties, FONT, style.getFontIndex());
putBoolean(properties, HIDDEN, style.getHidden());
putShort(properties, INDENTION, style.getIndention());
putShort(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor());
putBoolean(properties, LOCKED, style.getLocked());
putShort(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor());
putShort(properties, ROTATION, style.getRotation());
putShort(properties, TOP_BORDER_COLOR, style.getTopBorderColor());
putShort(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment());
putBoolean(properties, WRAP_TEXT, style.getWrapText());
return properties;
}
*
* @param style cell style
* @return map of format properties (String -> Object)
* @see #setFormatProperties(org.apache.poi.ss.usermodel.CellStyle, org.apache.poi.ss.usermodel.Workbook, java.util.Map)
*/
private static Map<String, Object> getFormatProperties(CellStyle style) {
Map<String, Object> properties = new HashMap<String, Object>();
putShort(properties, ALIGNMENT, style.getAlignment());
putBorderStyle(properties, BORDER_BOTTOM, style.getBorderBottom());
putBorderStyle(properties, BORDER_LEFT, style.getBorderLeft());
putBorderStyle(properties, BORDER_RIGHT, style.getBorderRight());
putBorderStyle(properties, BORDER_TOP, style.getBorderTop());
putShort(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor());
putShort(properties, DATA_FORMAT, style.getDataFormat());
putShort(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor());
putShort(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor());
putShort(properties, FILL_PATTERN, style.getFillPattern());
putShort(properties, FONT, style.getFontIndex());
putBoolean(properties, HIDDEN, style.getHidden());
putShort(properties, INDENTION, style.getIndention());
putShort(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor());
putBoolean(properties, LOCKED, style.getLocked());
putShort(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor());
putShort(properties, ROTATION, style.getRotation());
putShort(properties, TOP_BORDER_COLOR, style.getTopBorderColor());
putShort(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment());
putBoolean(properties, WRAP_TEXT, style.getWrapText());
return properties;
}
/**
* Sets the format properties of the given style based on the given map.
*
* @param style cell style
* @param workbook parent workbook
* @param properties map of format properties (String -> Object)
* @see #getFormatProperties(CellStyle)
*/
private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties) {
style.setAlignment(getShort(properties, ALIGNMENT));
style.setBorderBottom(getBorderStyle(properties, BORDER_BOTTOM));
style.setBorderLeft(getBorderStyle(properties, BORDER_LEFT));
style.setBorderRight(getBorderStyle(properties, BORDER_RIGHT));
style.setBorderTop(getBorderStyle(properties, BORDER_TOP));
style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR));
style.setDataFormat(getShort(properties, DATA_FORMAT));
style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
style.setFillPattern(getShort(properties, FILL_PATTERN));
style.setFont(workbook.getFontAt(getShort(properties, FONT)));
style.setHidden(getBoolean(properties, HIDDEN));
style.setIndention(getShort(properties, INDENTION));
style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR));
style.setLocked(getBoolean(properties, LOCKED));
style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR));
style.setRotation(getShort(properties, ROTATION));
style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR));
style.setVerticalAlignment(getShort(properties, VERTICAL_ALIGNMENT));
style.setWrapText(getBoolean(properties, WRAP_TEXT));
}
/**
* Sets the format properties of the given style based on the given map.
*
* @param style cell style
* @param workbook parent workbook
* @param properties map of format properties (String -> Object)
* @see #getFormatProperties(CellStyle)
*/
private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties) {
style.setAlignment(getShort(properties, ALIGNMENT));
style.setBorderBottom(getBorderStyle(properties, BORDER_BOTTOM));
style.setBorderLeft(getBorderStyle(properties, BORDER_LEFT));
style.setBorderRight(getBorderStyle(properties, BORDER_RIGHT));
style.setBorderTop(getBorderStyle(properties, BORDER_TOP));
style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR));
style.setDataFormat(getShort(properties, DATA_FORMAT));
style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
style.setFillPattern(getShort(properties, FILL_PATTERN));
style.setFont(workbook.getFontAt(getShort(properties, FONT)));
style.setHidden(getBoolean(properties, HIDDEN));
style.setIndention(getShort(properties, INDENTION));
style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR));
style.setLocked(getBoolean(properties, LOCKED));
style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR));
style.setRotation(getShort(properties, ROTATION));
style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR));
style.setVerticalAlignment(getShort(properties, VERTICAL_ALIGNMENT));
style.setWrapText(getBoolean(properties, WRAP_TEXT));
}
/**
* Utility method that returns the named short value form the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return zero if the property does not exist, or is not a {@link Short}
* otherwise the property value
*/
private static short getShort(Map<String, Object> properties, String name) {
Object value = properties.get(name);
if (value instanceof Short) {
return ((Short) value).shortValue();
}
return 0;
}
/**
* Utility method that returns the named BorderStyle value form the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return Border style if set, otherwise {@link BorderStyle#NONE}
*/
private static BorderStyle getBorderStyle(Map<String, Object> properties, String name) {
Object value = properties.get(name);
BorderStyle border;
if (value instanceof BorderStyle) {
border = (BorderStyle) value;
}
// @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future.
else if (value instanceof Short) {
short code = Short.valueOf((Short) value);
border = BorderStyle.valueOf(code);
}
else {
throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)");
}
return (border != null) ? border : BorderStyle.NONE;
}
/**
* Utility method that returns the named short value form the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return zero if the property does not exist, or is not a {@link Short}
* otherwise the property value
*/
private static short getShort(Map<String, Object> properties, String name) {
Object value = properties.get(name);
if (value instanceof Short) {
return ((Short) value).shortValue();
}
return 0;
}
/**
* Utility method that returns the named BorderStyle value form the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return Border style if set, otherwise {@link BorderStyle#NONE}
*/
private static BorderStyle getBorderStyle(Map<String, Object> properties, String name) {
Object value = properties.get(name);
BorderStyle border;
if (value instanceof BorderStyle) {
border = (BorderStyle) value;
}
// @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future.
else if (value instanceof Short) {
short code = Short.valueOf((Short) value);
border = BorderStyle.valueOf(code);
}
else {
throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)");
}
return (border != null) ? border : BorderStyle.NONE;
}
/**
* Utility method that returns the named boolean value form the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @return false if the property does not exist, or is not a {@link Boolean},
* true otherwise
*/
private static boolean getBoolean(Map<String, Object> properties, String name) {
Object value = properties.get(name);
//noinspection SimplifiableIfStatement
if (value instanceof Boolean) {
return ((Boolean) value).booleanValue();
}
return false;
}
/**
* Utility method that returns the named boolean value form the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @return false if the property does not exist, or is not a {@link Boolean},
* true otherwise
*/
private static boolean getBoolean(Map<String, Object> properties, String name) {
Object value = properties.get(name);
//noinspection SimplifiableIfStatement
if (value instanceof Boolean) {
return ((Boolean) value).booleanValue();
}
return false;
}
/**
* Utility method that puts the named short value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putShort(Map<String, Object> properties, String name, short value) {
properties.put(name, Short.valueOf(value));
}
/**
* Utility method that puts the named short value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putBorderStyle(Map<String, Object> properties, String name, BorderStyle border) {
properties.put(name, border);
}
/**
* Utility method that puts the named short value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putShort(Map<String, Object> properties, String name, short value) {
properties.put(name, Short.valueOf(value));
}
/**
* Utility method that puts the named short value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putBorderStyle(Map<String, Object> properties, String name, BorderStyle border) {
properties.put(name, border);
}
/**
* Utility method that puts the named boolean value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putBoolean(Map<String, Object> properties, String name, boolean value) {
properties.put(name, Boolean.valueOf(value));
}
/**
* Utility method that puts the named boolean value to the given map.
*
* @param properties map of properties (String -> Object)
* @param name property name
* @param value property value
*/
private static void putBoolean(Map<String, Object> properties, String name, boolean value) {
properties.put(name, Boolean.valueOf(value));
}
/**
* Looks for text in the cell that should be unicode, like &alpha; and provides the
* unicode version of it.
*
*@param cell The cell to check for unicode values
*@return translated to unicode
*/
public static Cell translateUnicodeValues(Cell cell) {
/**
* Looks for text in the cell that should be unicode, like &alpha; and provides the
* unicode version of it.
*
*@param cell The cell to check for unicode values
*@return translated to unicode
*/
public static Cell translateUnicodeValues(Cell cell) {
String s = cell.getRichStringCellValue().getString();
boolean foundUnicode = false;
String lowerCaseStr = s.toLowerCase(Locale.ROOT);
String s = cell.getRichStringCellValue().getString();
boolean foundUnicode = false;
String lowerCaseStr = s.toLowerCase(Locale.ROOT);
for (UnicodeMapping entry : unicodeMappings) {
String key = entry.entityName;
if (lowerCaseStr.contains(key)) {
s = s.replaceAll(key, entry.resolvedValue);
foundUnicode = true;
}
}
if (foundUnicode) {
cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
.createRichTextString(s));
}
return cell;
}
for (UnicodeMapping entry : unicodeMappings) {
String key = entry.entityName;
if (lowerCaseStr.contains(key)) {
s = s.replaceAll(key, entry.resolvedValue);
foundUnicode = true;
}
}
if (foundUnicode) {
cell.setCellValue(cell.getRow().getSheet().getWorkbook().getCreationHelper()
.createRichTextString(s));
}
return cell;
}
static {
unicodeMappings = new UnicodeMapping[] {
um("alpha", "\u03B1" ),
um("beta", "\u03B2" ),
um("gamma", "\u03B3" ),
um("delta", "\u03B4" ),
um("epsilon", "\u03B5" ),
um("zeta", "\u03B6" ),
um("eta", "\u03B7" ),
um("theta", "\u03B8" ),
um("iota", "\u03B9" ),
um("kappa", "\u03BA" ),
um("lambda", "\u03BB" ),
um("mu", "\u03BC" ),
um("nu", "\u03BD" ),
um("xi", "\u03BE" ),
um("omicron", "\u03BF" ),
};
}
static {
unicodeMappings = new UnicodeMapping[] {
um("alpha", "\u03B1" ),
um("beta", "\u03B2" ),
um("gamma", "\u03B3" ),
um("delta", "\u03B4" ),
um("epsilon", "\u03B5" ),
um("zeta", "\u03B6" ),
um("eta", "\u03B7" ),
um("theta", "\u03B8" ),
um("iota", "\u03B9" ),
um("kappa", "\u03BA" ),
um("lambda", "\u03BB" ),
um("mu", "\u03BC" ),
um("nu", "\u03BD" ),
um("xi", "\u03BE" ),
um("omicron", "\u03BF" ),
};
}
private static UnicodeMapping um(String entityName, String resolvedValue) {
return new UnicodeMapping(entityName, resolvedValue);
}
private static UnicodeMapping um(String entityName, String resolvedValue) {
return new UnicodeMapping(entityName, resolvedValue);
}
}

View File

@ -160,8 +160,8 @@ public class XSSFCellStyle implements CellStyle {
// bug 56295: ensure that the fills is available and set correctly
CTFill fill = CTFill.Factory.parse(
src.getCTFill().toString(), DEFAULT_XML_OPTIONS
);
src.getCTFill().toString(), DEFAULT_XML_OPTIONS
);
addFill(fill);
// bug 58084: set borders correctly
@ -203,19 +203,19 @@ public class XSSFCellStyle implements CellStyle {
}
}
private void addFill(CTFill fill) {
int idx = _stylesSource.putFill(new XSSFCellFill(fill));
private void addFill(CTFill fill) {
int idx = _stylesSource.putFill(new XSSFCellFill(fill));
_cellXf.setFillId(idx);
_cellXf.setApplyFill(true);
}
private void addBorder(CTBorder border) {
_cellXf.setFillId(idx);
_cellXf.setApplyFill(true);
}
private void addBorder(CTBorder border) {
int idx = _stylesSource.putBorder(new XSSFCellBorder(border, _theme));
_cellXf.setBorderId(idx);
_cellXf.setApplyBorder(true);
}
}
/**
* Get the type of horizontal alignment for the cell
@ -439,7 +439,7 @@ public class XSSFCellStyle implements CellStyle {
* @return XSSFColor - fill color or <code>null</code> if not set
*/
public XSSFColor getFillBackgroundXSSFColor() {
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
int fillIndex = (int)_cellXf.getFillId();
@ -478,7 +478,7 @@ public class XSSFCellStyle implements CellStyle {
* @return XSSFColor - fill color or <code>null</code> if not set
*/
public XSSFColor getFillForegroundXSSFColor() {
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
int fillIndex = (int)_cellXf.getFillId();
@ -515,7 +515,7 @@ public class XSSFCellStyle implements CellStyle {
*/
@Override
public short getFillPattern() {
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return 0;
int fillIndex = (int)_cellXf.getFillId();
@ -1064,7 +1064,7 @@ public class XSSFCellStyle implements CellStyle {
*/
private CTFill getCTFill(){
CTFill ct;
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
if(!_cellXf.isSetApplyFill() || _cellXf.getApplyFill()) {
int fillIndex = (int)_cellXf.getFillId();
XSSFCellFill cf = _stylesSource.getFillAt(fillIndex);
@ -1138,7 +1138,7 @@ public class XSSFCellStyle implements CellStyle {
* @see org.apache.poi.ss.usermodel.FillPatternType
*/
public void setFillPattern(FillPatternType ptrn) {
setFillPattern((short)ptrn.ordinal());
setFillPattern((short)ptrn.ordinal());
}
/**