public class DataFormatter
extends java.lang.Object
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 DataFormatter.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
DataFormatter.setDefaultNumberFormat(Format)
. Note: the
default format will only be used when a Format cannot be created from the
cell's data format string.
Note that by default formatted numeric values are trimmed. Excel formats can contain spacers and padding and the default behavior is to strip them off.
Example:
Consider a numeric cell with a value 12.343
and format "##.##_ "
.
The trailing underscore and space ("_ ") in the format adds a space to the end and Excel formats this cell as "12.34 "
,
but DataFormatter
trims the formatted value and returns "12.34"
.
emulateCsv=true
flag in the DateFormatter
cosntructor.
If set to true, then the output tries to conform to what you get when you take an xls or xlsx in Excel and Save As CSV file:
DataFormatter
will output "0".
Constructor and Description |
---|
DataFormatter()
Creates a formatter using the
default locale . |
DataFormatter(boolean emulateCsv)
Creates a formatter using the
default locale . |
DataFormatter(java.util.Locale locale)
Creates a formatter using the given locale.
|
DataFormatter(java.util.Locale locale,
boolean emulateCsv)
Creates a formatter using the given locale.
|
Modifier and Type | Method and Description |
---|---|
void |
addFormat(java.lang.String excelFormatStr,
java.text.Format format)
Adds a new format to the available formats.
|
java.text.Format |
createFormat(Cell cell)
Create and return a Format based on the format string from a cell's
style.
|
java.lang.String |
formatCellValue(Cell cell)
Returns the formatted value of a cell as a String regardless
of the cell type.
|
java.lang.String |
formatCellValue(Cell cell,
FormulaEvaluator evaluator)
Returns the formatted value of a cell as a String regardless
of the cell type.
|
java.lang.String |
formatCellValue(Cell cell,
int cellType) |
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.lang.String |
formatRawCellContents(double value,
int formatIndex,
java.lang.String formatString,
boolean use1904Windowing)
Formats the given raw cell value, based on the supplied
format index and string, according to excel style rules.
|
java.text.Format |
getDefaultFormat(Cell cell)
Returns a default format for a cell.
|
static java.text.Format |
getJavaFormat(Cell cell,
java.util.Locale locale) |
static java.text.Format |
getJavaFormat(double cellValue,
java.lang.String format,
java.util.Locale locale) |
void |
setDefaultNumberFormat(java.text.Format format)
Sets a default number format to be used when the Excel format cannot be
parsed successfully.
|
static void |
setExcelStyleRoundingMode(java.text.DecimalFormat format)
Enables excel style rounding mode (round half up)
on the Decimal Format if possible.
|
static void |
setExcelStyleRoundingMode(java.text.DecimalFormat format,
java.math.RoundingMode roundingMode)
Enables custom rounding mode
on the Decimal Format if possible.
|
public DataFormatter()
default locale
.public DataFormatter(boolean emulateCsv)
default locale
.emulateCsv
- whether to emulate CSV output.public DataFormatter(java.util.Locale locale, boolean emulateCsv)
emulateCsv
- whether to emulate CSV output.public DataFormatter(java.util.Locale locale)
public java.text.Format createFormat(Cell cell)
cell
- The Excel cellpublic java.text.Format getDefaultFormat(Cell cell)
cell
- The cellpublic java.lang.String formatRawCellContents(double value, int formatIndex, java.lang.String formatString)
formatCellValue(Cell)
public java.lang.String formatRawCellContents(double value, int formatIndex, java.lang.String formatString, boolean use1904Windowing)
formatCellValue(Cell)
public java.lang.String formatCellValue(Cell 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.
cell
- The cellpublic java.lang.String formatCellValue(Cell cell, FormulaEvaluator evaluator)
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
FormulaEvaluator
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
cell
- The cell (can be null)evaluator
- The FormulaEvaluator (can be null)public java.lang.String formatCellValue(Cell cell, int cellType)
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.
format
- A Format instance to be used as a defaultFormat.format(java.lang.Object)
public void addFormat(java.lang.String excelFormatStr, java.text.Format 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.
excelFormatStr
- The data format stringformat
- A Format instancepublic static void setExcelStyleRoundingMode(java.text.DecimalFormat format)
public static void setExcelStyleRoundingMode(java.text.DecimalFormat format, java.math.RoundingMode roundingMode)
format
- DecimalFormatroundingMode
- RoundingModepublic static final java.text.Format getJavaFormat(Cell cell, java.util.Locale locale)
public static final java.text.Format getJavaFormat(double cellValue, java.lang.String format, java.util.Locale locale)
Copyright © 2005-2010 Potix Corporation. All Rights Reserved.