HSSFCellUtilpublic class HSSFCellUtil extends Object 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. |
Fields Summary |
---|
private static HashMap | unicodeMappings |
Methods Summary |
---|
public static org.apache.poi.hssf.usermodel.HSSFCell | createCell(org.apache.poi.hssf.usermodel.HSSFRow row, int column, java.lang.String value, org.apache.poi.hssf.usermodel.HSSFCellStyle style)Creates a cell, gives it a value, and applies a style if provided
HSSFCell cell = getCell( row, column );
cell.setCellValue( value );
if ( style != null )
{
cell.setCellStyle( style );
}
return cell;
| public static org.apache.poi.hssf.usermodel.HSSFCell | createCell(org.apache.poi.hssf.usermodel.HSSFRow row, int column, java.lang.String value)Create a cell, and give it a value.
return createCell( row, column, value, null );
| public static org.apache.poi.hssf.usermodel.HSSFCell | getCell(org.apache.poi.hssf.usermodel.HSSFRow row, int column)Get a specific cell from a row. If the cell doesn't exist, then create it.
HSSFCell cell = row.getCell( (short) column );
if ( cell == null )
{
cell = row.createCell( (short) column );
}
return cell;
| public static org.apache.poi.hssf.usermodel.HSSFRow | getRow(int rowCounter, org.apache.poi.hssf.usermodel.HSSFSheet sheet)Get a row from the spreadsheet, and create it if it doesn't exist.
HSSFRow row = sheet.getRow( (short) rowCounter );
if ( row == null )
{
row = sheet.createRow( (short) rowCounter );
}
return row;
| public static void | setAlignment(org.apache.poi.hssf.usermodel.HSSFCell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook workbook, short align)Take a cell, and align it.
setCellStyleProperty( cell, workbook, "alignment", new Short( align ) );
| public static void | setCellStyleProperty(org.apache.poi.hssf.usermodel.HSSFCell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook workbook, java.lang.String propertyName, java.lang.Object propertyValue)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
creates a new one. If it does create a new one, then it applyies the
propertyName and propertyValue to the style. This is nessasary because
Excel has an upper limit on the number of Styles that it supports.
try
{
HSSFCellStyle originalStyle = cell.getCellStyle();
HSSFCellStyle newStyle = null;
Map values = PropertyUtils.describe( originalStyle );
values.put( propertyName, propertyValue );
values.remove( "index" );
// 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 );
Map wbStyleMap = PropertyUtils.describe( wbStyle );
wbStyleMap.remove( "index" );
if ( wbStyleMap.equals( values ) )
{
newStyle = wbStyle;
break;
}
}
if ( newStyle == null )
{
newStyle = workbook.createCellStyle();
newStyle.setFont( workbook.getFontAt( originalStyle.getFontIndex() ) );
PropertyUtils.copyProperties( newStyle, originalStyle );
PropertyUtils.setProperty( newStyle, propertyName, propertyValue );
}
cell.setCellStyle( newStyle );
}
catch ( Exception e )
{
e.printStackTrace();
throw new NestableException( "Couldn't setCellStyleProperty.", e );
}
| public static void | setFont(org.apache.poi.hssf.usermodel.HSSFCell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook workbook, org.apache.poi.hssf.usermodel.HSSFFont font)Take a cell, and apply a font to it
setCellStyleProperty( cell, workbook, "font", font );
| public static org.apache.poi.hssf.usermodel.HSSFCell | translateUnicodeValues(org.apache.poi.hssf.usermodel.HSSFCell cell)Looks for text in the cell that should be unicode, like α and provides the
unicode version of it.
String s = cell.getStringCellValue();
boolean foundUnicode = false;
for ( Iterator i = unicodeMappings.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = (Map.Entry) i.next();
String key = (String) entry.getKey();
if ( s.toLowerCase().indexOf( key ) != -1 )
{
s = StringUtils.replace( s, key, "" + entry.getValue().toString() + "" );
foundUnicode = true;
}
}
if ( foundUnicode )
{
cell.setEncoding( HSSFCell.ENCODING_UTF_16 );
cell.setCellValue( s );
}
return cell;
|
|