2002-09-29 19:50:35 -04:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
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
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
==================================================================== */
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
package org.apache.poi.hssf.usermodel.contrib;
|
2002-09-19 21:27:33 -04:00
|
|
|
|
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
|
2002-09-19 21:27:33 -04:00
|
|
|
/**
|
|
|
|
* Various utility functions that make working with a cells and rows easier. The various
|
|
|
|
* methods that deal with style's allow you to create your HSSFCellStyles as you need them.
|
|
|
|
* When you apply a style change to a cell, the code will attempt to see if a style already
|
|
|
|
* exists that meets your needs. If not, then it will create a new style. This is to prevent
|
|
|
|
* creating too many styles. there is an upper limit in Excel on the number of styles that
|
|
|
|
* can be supported.
|
|
|
|
*
|
|
|
|
*@author Eric Pugh epugh@upstate.com
|
|
|
|
*/
|
2008-07-16 14:47:13 -04:00
|
|
|
public final class HSSFCellUtil
|
2002-09-29 19:50:35 -04:00
|
|
|
{
|
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
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 final class UnicodeMapping {
|
|
|
|
|
|
|
|
public final String entityName;
|
|
|
|
public final String resolvedValue;
|
|
|
|
|
|
|
|
public UnicodeMapping(String pEntityName, String pResolvedValue) {
|
|
|
|
entityName = "&" + pEntityName + ";";
|
|
|
|
resolvedValue = pResolvedValue;
|
|
|
|
}
|
|
|
|
}
|
2002-09-29 19:50:35 -04:00
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
private HSSFCellUtil() {
|
|
|
|
// no instances of this class
|
|
|
|
}
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a row from the spreadsheet, and create it if it doesn't exist.
|
|
|
|
*
|
|
|
|
*@param rowCounter The 0 based row number
|
|
|
|
*@param sheet The sheet that the row is part of.
|
|
|
|
*@return The row indicated by the rowCounter
|
|
|
|
*/
|
|
|
|
public static HSSFRow getRow( int rowCounter, HSSFSheet sheet )
|
|
|
|
{
|
2008-05-06 11:38:08 -04:00
|
|
|
HSSFRow row = sheet.getRow( rowCounter );
|
2002-09-29 19:50:35 -04:00
|
|
|
if ( row == null )
|
|
|
|
{
|
2008-05-06 11:38:08 -04:00
|
|
|
row = sheet.createRow( rowCounter );
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-05-06 11:38:08 -04:00
|
|
|
* Get a specific cell from a row. If the cell doesn't exist,
|
|
|
|
* then create it.
|
2002-09-29 19:50:35 -04:00
|
|
|
*
|
|
|
|
*@param row The row that the cell is part of
|
|
|
|
*@param column The column index that the cell is in.
|
|
|
|
*@return The cell indicated by the column.
|
|
|
|
*/
|
|
|
|
public static HSSFCell getCell( HSSFRow row, int column )
|
|
|
|
{
|
2008-05-06 11:38:08 -04:00
|
|
|
HSSFCell cell = row.getCell( column );
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
if ( cell == null )
|
|
|
|
{
|
2008-08-25 18:41:08 -04:00
|
|
|
cell = row.createCell(column );
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
|
|
|
return cell;
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 HSSFCell
|
|
|
|
*/
|
|
|
|
public static HSSFCell createCell( HSSFRow row, int column, String value, HSSFCellStyle style )
|
|
|
|
{
|
|
|
|
HSSFCell cell = getCell( row, column );
|
|
|
|
|
2008-05-06 11:38:08 -04:00
|
|
|
cell.setCellValue(new HSSFRichTextString(value));
|
2002-09-29 19:50:35 -04:00
|
|
|
if ( style != null )
|
|
|
|
{
|
|
|
|
cell.setCellStyle( style );
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
/**
|
|
|
|
* 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 HSSFCell.
|
|
|
|
*/
|
|
|
|
public static HSSFCell createCell( HSSFRow row, int column, String value )
|
|
|
|
{
|
|
|
|
return createCell( row, column, value, null );
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2002-10-08 20:05:55 -04:00
|
|
|
*
|
|
|
|
* @see HSSFCellStyle for alignment options
|
2002-09-29 19:50:35 -04:00
|
|
|
*/
|
2008-07-16 14:47:13 -04:00
|
|
|
public static void setAlignment( HSSFCell cell, HSSFWorkbook workbook, short align )
|
2002-09-29 19:50:35 -04:00
|
|
|
{
|
2008-07-16 14:47:13 -04:00
|
|
|
setCellStyleProperty( cell, workbook, ALIGNMENT, new Short( align ) );
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
/**
|
|
|
|
* 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 HSSFFont that you want to set...
|
|
|
|
*/
|
2008-07-16 14:47:13 -04:00
|
|
|
public static void setFont( HSSFCell cell, HSSFWorkbook workbook, HSSFFont font )
|
2002-09-29 19:50:35 -04:00
|
|
|
{
|
2008-07-16 14:47:13 -04:00
|
|
|
setCellStyleProperty( cell, workbook, FONT, font );
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
/**
|
|
|
|
* This method attempt to find an already existing HSSFCellStyle that matches
|
|
|
|
* what you want the style to be. If it does not find the style, then it
|
2008-07-16 14:47:13 -04:00
|
|
|
* creates a new one. If it does create a new one, then it applies the
|
|
|
|
* propertyName and propertyValue to the style. This is necessary because
|
2002-09-29 19:50:35 -04:00
|
|
|
* Excel has an upper limit on the number of Styles that it supports.
|
|
|
|
*
|
|
|
|
*@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
|
|
|
|
*@exception NestableException Thrown if an error happens.
|
|
|
|
*/
|
|
|
|
public static void setCellStyleProperty( HSSFCell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue )
|
|
|
|
{
|
|
|
|
HSSFCellStyle originalStyle = cell.getCellStyle();
|
|
|
|
HSSFCellStyle newStyle = null;
|
2008-07-16 14:47:13 -04:00
|
|
|
Map values = getFormatProperties( originalStyle );
|
2002-09-29 19:50:35 -04:00
|
|
|
values.put( propertyName, propertyValue );
|
|
|
|
|
|
|
|
// index seems like what index the cellstyle is in the list of styles for a workbook.
|
|
|
|
// not good to compare on!
|
|
|
|
short numberCellStyles = workbook.getNumCellStyles();
|
|
|
|
|
|
|
|
for ( short i = 0; i < numberCellStyles; i++ )
|
|
|
|
{
|
|
|
|
HSSFCellStyle wbStyle = workbook.getCellStyleAt( i );
|
2008-07-16 14:47:13 -04:00
|
|
|
Map wbStyleMap = getFormatProperties( wbStyle );
|
2002-09-29 19:50:35 -04:00
|
|
|
|
|
|
|
if ( wbStyleMap.equals( values ) )
|
|
|
|
{
|
|
|
|
newStyle = wbStyle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( newStyle == null )
|
|
|
|
{
|
|
|
|
newStyle = workbook.createCellStyle();
|
2008-07-16 14:47:13 -04:00
|
|
|
setFormatProperties( newStyle, workbook, values );
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cell.setCellStyle( newStyle );
|
2008-07-16 14:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a map containing the format properties of the given cell style.
|
|
|
|
*
|
|
|
|
* @param style cell style
|
|
|
|
* @return map of format properties (String -> Object)
|
|
|
|
* @see #setFormatProperties(HSSFCellStyle, Map)
|
|
|
|
*/
|
|
|
|
private static Map getFormatProperties(HSSFCellStyle style) {
|
|
|
|
Map properties = new HashMap();
|
|
|
|
putShort( properties, ALIGNMENT, style.getAlignment() );
|
|
|
|
putShort( properties, BORDER_BOTTOM, style.getBorderBottom() );
|
|
|
|
putShort( properties, BORDER_LEFT, style.getBorderLeft() );
|
|
|
|
putShort( properties, BORDER_RIGHT, style.getBorderRight() );
|
|
|
|
putShort( 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(HSSFCellStyle)
|
|
|
|
*/
|
|
|
|
private static void setFormatProperties(
|
|
|
|
HSSFCellStyle style, HSSFWorkbook workbook, Map properties) {
|
|
|
|
style.setAlignment( getShort( properties, ALIGNMENT ) );
|
|
|
|
style.setBorderBottom( getShort( properties, BORDER_BOTTOM ) );
|
|
|
|
style.setBorderLeft( getShort( properties, BORDER_LEFT ) );
|
|
|
|
style.setBorderRight( getShort( properties, BORDER_RIGHT ) );
|
|
|
|
style.setBorderTop( getShort( 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.
|
|
|
|
* Returns zero if the property does not exist, or is not a {@link Short}.
|
|
|
|
*
|
|
|
|
* @param properties map of named properties (String -> Object)
|
|
|
|
* @param name property name
|
|
|
|
* @return property value, or zero
|
|
|
|
*/
|
|
|
|
private static short getShort(Map properties, String name) {
|
|
|
|
Object value = properties.get( name );
|
|
|
|
if ( value instanceof Short ) {
|
|
|
|
return ((Short) value).shortValue();
|
|
|
|
} else {
|
|
|
|
return 0;
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
2008-07-16 14:47:13 -04:00
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
/**
|
|
|
|
* Utility method that returns the named boolean value form the given map.
|
|
|
|
* Returns false if the property does not exist, or is not a {@link Boolean}.
|
|
|
|
*
|
|
|
|
* @param properties map of properties (String -> Object)
|
|
|
|
* @param name property name
|
|
|
|
* @return property value, or false
|
|
|
|
*/
|
|
|
|
private static boolean getBoolean(Map properties, String name) {
|
|
|
|
Object value = properties.get( name );
|
|
|
|
if ( value instanceof Boolean ) {
|
|
|
|
return ((Boolean) value).booleanValue();
|
|
|
|
} else {
|
|
|
|
return false;
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
/**
|
|
|
|
* 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 properties, String name, short value) {
|
|
|
|
properties.put( name, new Short( 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 properties, String name, boolean value) {
|
|
|
|
properties.put( name, new Boolean( value ) );
|
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2002-09-29 19:50:35 -04:00
|
|
|
/**
|
|
|
|
* Looks for text in the cell that should be unicode, like α and provides the
|
|
|
|
* unicode version of it.
|
|
|
|
*
|
|
|
|
*@param cell The cell to check for unicode values
|
|
|
|
*@return transalted to unicode
|
|
|
|
*/
|
|
|
|
public static HSSFCell translateUnicodeValues( HSSFCell cell )
|
|
|
|
{
|
|
|
|
|
2008-05-06 11:38:08 -04:00
|
|
|
String s = cell.getRichStringCellValue().getString();
|
2002-09-29 19:50:35 -04:00
|
|
|
boolean foundUnicode = false;
|
2008-07-16 14:47:13 -04:00
|
|
|
String lowerCaseStr = s.toLowerCase();
|
2002-09-29 19:50:35 -04:00
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
for (int i = 0; i < unicodeMappings.length; i++) {
|
|
|
|
UnicodeMapping entry = unicodeMappings[i];
|
|
|
|
String key = entry.entityName;
|
|
|
|
if ( lowerCaseStr.indexOf( key ) != -1 )
|
2002-09-29 19:50:35 -04:00
|
|
|
{
|
2008-07-16 14:47:13 -04:00
|
|
|
s = s.replaceAll(key, entry.resolvedValue);
|
2002-09-29 19:50:35 -04:00
|
|
|
foundUnicode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( foundUnicode )
|
|
|
|
{
|
2008-07-16 14:47:13 -04:00
|
|
|
cell.setCellValue(new HSSFRichTextString(s));
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
|
|
|
return cell;
|
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2003-05-03 14:58:44 -04:00
|
|
|
static {
|
2008-07-16 14:47:13 -04:00
|
|
|
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" ),
|
|
|
|
};
|
2002-09-29 19:50:35 -04:00
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
|
2008-07-16 14:47:13 -04:00
|
|
|
private static UnicodeMapping um(String entityName, String resolvedValue) {
|
|
|
|
return new UnicodeMapping(entityName, resolvedValue);
|
|
|
|
}
|
2002-09-19 21:27:33 -04:00
|
|
|
}
|