FileDocCategorySizeDatePackage
HSSFCellUtil.javaAPI DocApache Poi 3.0.19444Mon Jan 01 12:39:34 GMT 2007org.apache.poi.hssf.usermodel.contrib

HSSFCellUtil

public 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.
author
Eric Pugh epugh@upstate.com

Fields Summary
private static HashMap
unicodeMappings
Constructors Summary
Methods Summary
public static org.apache.poi.hssf.usermodel.HSSFCellcreateCell(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

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

        HSSFCell cell = getCell( row, column );

        cell.setCellValue( value );
        if ( style != null )
        {
            cell.setCellStyle( style );
        }

        return cell;
    
public static org.apache.poi.hssf.usermodel.HSSFCellcreateCell(org.apache.poi.hssf.usermodel.HSSFRow row, int column, java.lang.String value)
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.

        return createCell( row, column, value, null );
    
public static org.apache.poi.hssf.usermodel.HSSFCellgetCell(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.

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.

        HSSFCell cell = row.getCell( (short) column );

        if ( cell == null )
        {
            cell = row.createCell( (short) column );
        }
        return cell;
    
public static org.apache.poi.hssf.usermodel.HSSFRowgetRow(int rowCounter, org.apache.poi.hssf.usermodel.HSSFSheet sheet)
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



                                                                 
            
    
        HSSFRow row = sheet.getRow( (short) rowCounter );
        if ( row == null )
        {
            row = sheet.createRow( (short) rowCounter );
        }

        return row;
    
public static voidsetAlignment(org.apache.poi.hssf.usermodel.HSSFCell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook workbook, short 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.
exception
NestableException Thrown if an error happens.
see
HSSFCellStyle for alignment options

        setCellStyleProperty( cell, workbook, "alignment", new Short( align ) );
    
public static voidsetCellStyleProperty(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.

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.

        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 voidsetFont(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

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...
exception
NestableException Thrown if an error happens.

        setCellStyleProperty( cell, workbook, "font", font );
    
public static org.apache.poi.hssf.usermodel.HSSFCelltranslateUnicodeValues(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.

param
cell The cell to check for unicode values
return
transalted to unicode


        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;