Class DataFormatter
- All Implemented Interfaces:
Observer
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:
- SSN "000-00-0000"
- Phone Number "(###) ###-####"
- Zip plus 4 "00000-0000"
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:
- returned values are not trimmed
- Invalid dates are formatted as 255 pound signs ("#")
- simulate Excel's handling of a format string of all # when the value is 0.
Excel will output "",
DataFormatterwill output "0".
Some formats are automatically "localized" by Excel, eg show as mm/dd/yyyy when
loaded in Excel in some Locales but as dd/mm/yyyy in others. These are always
returned in the "default" (US) format, as stored in the file.
Some format strings request an alternate locale, eg
[$-809]d/m/yy h:mm AM/PM which explicitly requests UK locale.
These locale directives are (currently) ignored.
You can use DateFormatConverter to do some of this localisation if
you need it.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classFormat class that does nothing and always returns a constant string.private classprivate static final classFormat class for Excel phone number format.private static final classFormat class for Excel's SSN format.private static final classFormat class for Excel Zip + 4 format. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PatternA regex to detect if an alternate grouping character is used in a numeric formatprivate static final PatternPattern to find "AM/PM" markerprivate static final PatternA regex to match the colour formattings rules.private DateFormatSymbolsThe date symbols of the locale used for formatting values.private static final PatternPattern to find days of week as text "ddd...."private DecimalFormatSymbolsThe decimal symbols of the locale used for formatting values.private DateFormatA default date format, if no date format was givenprivate static final Stringprivate static final Stringprivate FormatA default format to use when a number pattern cannot be parsed.private final booleanA map to cache formats.private static final PatternA regex to identify a fraction pattern.private static final PatternA regex to strip junk out of fraction formatsprivate FormatGeneral format for numbers.private static final StringCells formatted with a date or time format and which contain invalid date or time values show 255 pound signs ("#").private Localestores the locale valid it the last formatting callprivate final DataFormatter.LocaleChangeObservablethe Observable to notify, when the locale has been changedprivate booleanstores if the locale should change according toLocaleUtil.getUserLocale()private static final PatternA regex to find locale patterns like [$$-1009] and [$?-452].protected static org.slf4j.LoggerFor logging any problems we findprivate static final PatternPattern to find a number format: "0" or "#" -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a formatter using thedefault locale.DataFormatter(boolean emulateCSV) Creates a formatter using thedefault locale.DataFormatter(Locale locale) Creates a formatter using the given locale.DataFormatter(Locale locale, boolean emulateCSV) Creates a formatter using the given locale.privateDataFormatter(Locale locale, boolean localeIsAdapting, boolean emulateCSV) Creates a formatter using the given locale. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new format to the available formats.private StringcleanFormatForNumber(String formatStr) private FormatcreateDateFormat(String pFormatStr, double cellValue) private FormatcreateFormat(double cellValue, long formatIndex, String sFormat) createFormat(Cell cell) Create and return a Format based on the format string from a cell's style.private static DecimalFormatprivate FormatcreateNumberFormat(String formatStr, double cellValue) formatCellValue(Cell cell) Returns the formatted value of a cell as a String regardless of the cell type.formatRawCellContents(double value, int formatIndex, String formatString) Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.getCellFormula(Cell _cell) Return a formula for the cell, for example,SUM(C4:E4)private Stringprivate FormatgetDefaultFormat(double cellValue) getDefaultFormat(Cell cell) Returns a default format for a cell.private Formatprivate FormatReturn a Format for the given cell if one exists, otherwise try to create one.private StringgetFormattedDateString(Cell cell) Returns the formatted value of an Excel date as a String based on the cell'sDataFormat.private StringgetFormattedNumberString(Cell cell) Returns the formatted value of an Excel number as a String based on the cell'sDataFormat.If the Locale has been changed viaLocaleUtil.setUserLocale(Locale)the stored formats need to be refreshed.private StringperformDateFormatting(Date d, Format dateFormat) Performs Excel-style date formatting, using the supplied Date and formatvoidsetDefaultNumberFormat(Format format) Sets a default number format to be used when the Excel format cannot be parsed successfully.static voidEnables excel style rounding mode (round half up) on the Decimal Format given.static voidsetExcelStyleRoundingMode(DecimalFormat format, RoundingMode roundingMode) Enables custom rounding mode on the given Decimal Format.voidupdate(Observable observable, Object localeObj) Update formats when locale has been changed
-
Field Details
-
defaultFractionWholePartFormat
- See Also:
-
defaultFractionFractionPartFormat
- See Also:
-
numPattern
Pattern to find a number format: "0" or "#" -
daysAsText
Pattern to find days of week as text "ddd...." -
amPmPattern
Pattern to find "AM/PM" marker -
localePatternGroup
A regex to find locale patterns like [$$-1009] and [$?-452]. Note that we don't currently process these into locales -
colorPattern
A regex to match the colour formattings rules. Allowed colours are: Black, Blue, Cyan, Green, Magenta, Red, White, Yellow, "Color n" (1<=n<=56) -
fractionPattern
A regex to identify a fraction pattern. This requires that replaceAll("\\?", "#") has already been called -
fractionStripper
A regex to strip junk out of fraction formats -
alternateGrouping
A regex to detect if an alternate grouping character is used in a numeric format -
invalidDateTimeString
Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs ("#"). -
decimalSymbols
The decimal symbols of the locale used for formatting values. -
dateSymbols
The date symbols of the locale used for formatting values. -
defaultDateformat
A default date format, if no date format was given -
generalNumberFormat
General format for numbers. -
defaultNumFormat
A default format to use when a number pattern cannot be parsed. -
formats
A map to cache formats. Map<String,Format> formats -
emulateCSV
private final boolean emulateCSV -
locale
stores the locale valid it the last formatting call -
localeIsAdapting
private boolean localeIsAdaptingstores if the locale should change according toLocaleUtil.getUserLocale() -
localeChangedObservable
the Observable to notify, when the locale has been changed -
logger
protected static org.slf4j.Logger loggerFor logging any problems we find
-
-
Constructor Details
-
DataFormatter
public DataFormatter()Creates a formatter using thedefault locale. -
DataFormatter
public DataFormatter(boolean emulateCSV) Creates a formatter using thedefault locale.- Parameters:
emulateCSV- whether to emulate CSV output.
-
DataFormatter
Creates a formatter using the given locale. -
DataFormatter
Creates a formatter using the given locale.- Parameters:
emulateCSV- whether to emulate CSV output.
-
DataFormatter
Creates a formatter using the given locale.- Parameters:
localeIsAdapting- (true only if locale is not user-specified)emulateCSV- whether to emulate CSV output.
-
-
Method Details
-
getFormat
Return a Format for the given cell if one exists, otherwise try to create one. This method will returnnullif the any of the following is true:- the cell's style is null
- the style's data format string is null or empty
- the format string cannot be recognized as either a number or date
- Parameters:
cell- The cell to retrieve a Format for- Returns:
- A Format for the format String
-
getFormat
-
createFormat
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.
-
createFormat
-
createDateFormat
-
cleanFormatForNumber
-
createNumberFormat
-
getDefaultFormat
Returns a default format for a cell.- Parameters:
cell- The cell- Returns:
- a default format
-
getDefaultFormat
-
performDateFormatting
Performs Excel-style date formatting, using the supplied Date and format -
getFormattedDateString
Returns the formatted value of an Excel date as a String based on the cell'sDataFormat. i.e. "Thursday, January 02, 2003" , "01/02/2003" , "02-Jan" , etc.- Parameters:
cell- The cell- Returns:
- a formatted date string
-
getFormattedNumberString
Returns the formatted value of an Excel number as a String based on the cell'sDataFormat. Supported formats include currency, percents, decimals, phone number, SSN, etc.: "61.54%", "$100.00", "(800) 555-1234".- Parameters:
cell- The cell- Returns:
- a formatted number string
-
formatRawCellContents
Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.- See Also:
-
formatRawCellContents
public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) Formats the given raw cell value, based on the supplied format index and string, according to excel style rules.- See Also:
-
formatCellValue
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
-
getCellStringValue
-
getCellFormula
Return a formula for the cell, for example,SUM(C4:E4)- Returns:
- a formula for the cell
- Throws:
IllegalStateException- if the cell type returned by#getCellType()is not CELL_TYPE_FORMULA
-
setDefaultNumberFormat
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 aNumbervalue.- Parameters:
format- A Format instance to be used as a default- See Also:
-
addFormat
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 aNumbervalue.- Parameters:
excelFormatStr- The data format stringformat- A Format instance
-
createIntegerOnlyFormat
- Returns:
- a DecimalFormat with parseIntegerOnly set
true
-
setExcelStyleRoundingMode
Enables excel style rounding mode (round half up) on the Decimal Format given. -
setExcelStyleRoundingMode
Enables custom rounding mode on the given Decimal Format.- Parameters:
format- DecimalFormatroundingMode- RoundingMode
-
getLocaleChangedObservable
If the Locale has been changed viaLocaleUtil.setUserLocale(Locale)the stored formats need to be refreshed. All formats which aren't originated from DataFormatter itself, i.e. all Formats added viaaddFormat(String, Format)andsetDefaultNumberFormat(Format), need to be added again. To notify callers, the returnedObservableshould be used. The Object inObserver.update(Observable, Object)is the new Locale.- Returns:
- the listener object, where callers can register themselves
-
update
Update formats when locale has been changed
-