views:

130

answers:

5

For a Java application that drags data together from some sources and does some calculation itself, we want to offer users the possibility to use their own format strings, and would prefer the format string syntax they know from Excel (e. g. "$ "#,###.,0) which happens to be the same used in .net and Analysis Services.

The closest thing to use that I found in Java is DecimalFormat which lacks some of the features (e. g. thousands and millions formatting by putting the thousands separator at the end, the floating point numbers NaN and infinity are displayed differently, etc., and probably depending on locale, there will be some additional small differences.

For now, just the numeric formatting would suffice. Maybe date and string formatting will become a requirement in future.

Is there a library, or would we have to develop that ourselves?

I cannot imagine we are the only ones who have this issue.

POI as suggested by Noel M does not seem to offer a solution. Any other ideas?

+3  A: 

Apache POI might be able to do what you're looking for.

Noel M
I thought it just manages MS Office files. But maybe a side effect is number formatting. I will have a look.
Frank
@Frank - HSSF is the component for Excel: http://poi.apache.org/spreadsheet/index.html
Noel M
@Noel M - It does not seem that POI can help here. As far as I see, POI does _not_ itself offer any format evaluation. It just allows you to define formats, and assign them to cells. The real number to display string conversion then seems to be done only by Excel, and never in POI itself.
Frank
+3  A: 

No library exist for Java that does this kind of parsing. Reverse-engineering Excel to cover all test cases would be time consuming for such a niche product.

Since .net will convert 100% of your test cases, I suggest you focus on calling .Net in your Java application.

Thierry-Dimitri Roy
Calling .net from Java is one of the options we discuss currently. But as this is meant for an application to be deployed at different customers, a huge downside would be the added complexity of the deployment process.
Frank
A: 

(Adding another answer as you refer to my first in your question now)

It seems that such a facility doesn't exist. One thing you might be able to explore is to create it yourself. You could possibly use JavaCC to express these formats yourself, and how they end up mapping to typical Java objects. JavaCC would definitely be able to give you the ability to use your own format strings.

Perhaps this would fit in nicely as an implementation of NumberFormat

Noel M
Apart from calling .net, this is another possibility we are discussing. I am not sure, however, if for the simple task of parsing a format string a compiler generator is not too much overhead, and just hand-coding this would not be more easy, provided that nobody in the team so far has used JavaCC.
Frank
@Frank What a perfect CV-enhancing opportunity to learn!
Noel M
A: 

for decimal format including locale and other stuff:

http://www.jdocs.com/javase/7.b12/java/text/DecimalFormat.html

http://www.freshsources.com/Format.htm

http://www.java2s.com/Code/Java/I18N/JavaI18NFormatNumberFormat.htm

You can tweak the decimalFormat output using your own custom DecimalFormatSymbols. It's posible to set the string used to represent NaN and a lot more.

http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormatSymbols.html

for Date Format:

http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/

for both in a JFormattedTextField

http://www.java2s.com/Code/Java/Swing-JFC/Formatted-TextField.htm

text format

http://www.java2s.com/Code/Java/Development-Class/MakecustomInputTextFormatterinJava.htm http://www.java2s.com/Code/Java/Development-Class/Formatter.htm http://www.java2s.com/Code/Java/Development-Class/ApplyamasktoString.htm

Hector
While the JDK functionality offers formatting, there are some differences to the .net/Excel/AS formatting. And I am not sure if it would not be more effort to try to adapt the existing functionality than to develop a formatter completely from scratch.I would think, however, that looking at the existing formatters can give you some ideas how a good API of a custom class might look like.
Frank
+2  A: 

Is there a library, or would we have to develop that ourselves?

Check ExtenXLS, the Java Spreadsheet SDK, or its open source version OpenXLS (under GPLv.3). About OpenXLS:

OpenXLS is the open-source version of ExtenXLS -- the leading Java Spreadsheet SDK.

OpenXLS is the no-risk way to embed advanced Java spreadsheet functionality in your applications.

With the same industrial-strength code as ExtenXLS3, OpenXLS is a rock-solid performer which gives your Java applications the spreadsheet functionality your users demand -- and then some!

Give your users business intelligence applications and reports in the format they know and love, complete with Charts, images, VB code, and all of the features of Excel intact.

I had a quick look at the API and found this FormatHandle#convertFormatString(String) that converts an Excel-style format string to a Java Format string.

I don't know if it will match your constraints but it's definitely a serious product.

Pascal Thivent
This is the closest answer to what I expected when asking my question - even if we will not use it - as the licensing is either GPL (hence not usable in our commercial product) or has a commercial license (which is too expensive for the small use we will make of a full spreadsheet library).
Frank