org.apache.poi.hssf.usermodel
Class HSSFDataFormatter

java.lang.Object
  extended by org.apache.poi.hssf.usermodel.HSSFDataFormatter

public final class HSSFDataFormatter
extends java.lang.Object

HSSFDataFormatter contains methods for formatting the value stored in an HSSFCell. This can be useful for reports and GUI presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN, percentages, decimals, dates, phone numbers, zip codes, etc.

Internally, formats will be implemented using subclasses of Format such as DecimalFormat and SimpleDateFormat. Therefore the formats used by this class must obey the same pattern rules as these Format subclasses. This means that only legal number pattern characters ("0", "#", ".", "," etc.) may appear in number formats. Other characters can be inserted before or after the number pattern to form a prefix or suffix.

For example the Excel pattern "$#,##0.00 "USD"_);($#,##0.00 "USD")" will be correctly formatted as "$1,000.00 USD" or "($1,000.00 USD)". However the pattern "00-00-00" is incorrectly formatted by DecimalFormat as "000000--". For Excel formats that are not compatible with DecimalFormat, you can provide your own custom Format implementation via HSSFDataFormatter.addFormat(String,Format). The following custom formats are already provided by this class:

 
 

If the Excel format pattern cannot be parsed successfully, then a default format will be used. The default number format will mimic the Excel General format: "#" for whole numbers and "#.##########" for decimal numbers. You can override the default format pattern with HSSFDataFormatter.setDefaultNumberFormat(Format). Note: the default format will only be used when a Format cannot be created from the cell's data format string.

Author:
James May (james dot may at fmr dot com)

Constructor Summary
HSSFDataFormatter()
          Constructor
 
Method Summary
 void addFormat(java.lang.String excelFormatStr, java.text.Format format)
          Adds a new format to the available formats.
 java.text.Format createFormat(HSSFCell cell)
          Create and return a Format based on the format string from a cell's style.
 java.lang.String formatCellValue(HSSFCell cell)
           Returns the formatted value of a cell as a String regardless of the cell type.
 java.lang.String formatCellValue(HSSFCell cell, HSSFFormulaEvaluator evaluator)
           Returns the formatted value of a cell as a String regardless of the cell type.
 java.lang.String formatRawCellContents(double value, int formatIndex, java.lang.String formatString)
          Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.
 java.text.Format getDefaultFormat(HSSFCell cell)
          Returns a default format for a cell.
 void setDefaultNumberFormat(java.text.Format format)
           Sets a default number format to be used when the Excel format cannot be parsed successfully.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HSSFDataFormatter

public HSSFDataFormatter()
Constructor

Method Detail

createFormat

public java.text.Format createFormat(HSSFCell cell)
Create and return a Format based on the format string from a cell's style. If the pattern cannot be parsed, return a default pattern.

Parameters:
cell - The Excel cell
Returns:
A Format representing the excel format. May return null.

getDefaultFormat

public java.text.Format getDefaultFormat(HSSFCell cell)
Returns a default format for a cell.

Parameters:
cell - The cell
Returns:
a default format

formatRawCellContents

public java.lang.String formatRawCellContents(double value,
                                              int formatIndex,
                                              java.lang.String formatString)
Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.

See Also:
formatCellValue(HSSFCell)

formatCellValue

public java.lang.String formatCellValue(HSSFCell cell)

Returns the formatted value of a cell as a String regardless of the cell type. If the Excel format pattern cannot be parsed then the cell value will be formatted using a default format.

When passed a null or blank cell, this method will return an empty String (""). Formulas in formula type cells will not be evaluated.

Parameters:
cell - The cell
Returns:
the formatted cell value as a String

formatCellValue

public java.lang.String formatCellValue(HSSFCell cell,
                                        HSSFFormulaEvaluator evaluator)
                                 throws java.lang.IllegalArgumentException

Returns the formatted value of a cell as a String regardless of the cell type. If the Excel format pattern cannot be parsed then the cell value will be formatted using a default format.

When passed a null or blank cell, this method will return an empty String (""). Formula cells will be evaluated using the given HSSFFormulaEvaluator if the evaluator is non-null. If the evaluator is null, then the formula String will be returned. The caller is responsible for setting the currentRow on the evaluator

Parameters:
cell - The cell (can be null)
evaluator - The HSSFFormulaEvaluator (can be null)
Returns:
a string value of the cell
Throws:
java.lang.IllegalArgumentException

setDefaultNumberFormat

public void setDefaultNumberFormat(java.text.Format format)

Sets a default number format to be used when the Excel format cannot be parsed successfully. Note: This is a fall back for when an error occurs while parsing an Excel number format pattern. This will not affect cells with the General format.

The value that will be passed to the Format's format method (specified by java.text.Format#format) will be a double value from a numeric cell. Therefore the code in the format method should expect a Number value.

Parameters:
format - A Format instance to be used as a default
See Also:
Format.format(java.lang.Object)

addFormat

public void addFormat(java.lang.String excelFormatStr,
                      java.text.Format format)
Adds a new format to the available formats.

The value that will be passed to the Format's format method (specified by java.text.Format#format) will be a double value from a numeric cell. Therefore the code in the format method should expect a Number value.

Parameters:
excelFormatStr - The data format string
format - A Format instance


Copyright 2008 The Apache Software Foundation or its licensors, as applicable.