Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7166: Excel export currency cells #7169

Merged
merged 1 commit into from Apr 10, 2021
Merged

Conversation

melloware
Copy link
Member

@melloware melloware commented Mar 25, 2021

I learned more about currency than I ever could have imagined.

Now in a Datatable/TreeTable you can do this..

                    <p:column styleClass="p-text-right">
                        <f:facet name="header">
                            <h:outputText value="Price"/>
                        </f:facet>
                        <h:outputText value="#{product.price}">
                            <f:convertNumber type="currency" />
                        </h:outputText>
                    </p:column>

Which will display like this depending on locale below is US and then DE.
image

image

Then the new CurrencyValidator I borrowed from Commons Validator but had to heavily modify for our needs handles detecting and parsing the currency. And it also handles telling Excel what your currency format is all based on your Locale!

So now these cells are real currency cells that you can do Math and formulas on.
image

@stolp
Copy link
Contributor

stolp commented Mar 25, 2021

This is great.

For the sake of completion: This will also work with explicit currencySymbol and locale attributes in convertNumber?

@melloware
Copy link
Member Author

@stolp I don't think it will. Because I am using locale = LocaleUtils.getCurrentLocale(context); to detect your current locale. What gets passed to the Exporter is just an already calculated value.

I think we would have to tweak the Exporter.exportValue() method which is how it gets the value from the cell. So right now my method is receving the already parsed string from the <h:outputText

@stolp
Copy link
Contributor

stolp commented Mar 25, 2021

I did some research and now I'm confused by your reply.

My assumption is that in case that a currency symbol is present in the value (I think it is there), so CurrencyValidator.parse will not take the locale attribute in account in its first attempt to parse it. Only if the currency symbol is missing the locale is taking into account.

See: https://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/routines/CurrencyValidator.html#parse-java.lang.String-java.text.Format-

So I now assume that both currencySymbol and locale scenarios should work as well.

Or am I missing something?

@melloware
Copy link
Member Author

No you are correct the problem is I am generating 1 excel CellStyle for currency for the whole sheet using your global locale. locale = LocaleUtils.getCurrentLocale(context);

However if you had 2 columns in your datatable like this...

                    <p:column styleClass="p-text-right" headerText="US">
                        <h:outputText value="#{product.price}">
                            <f:convertNumber type="currency" currencySymbol="$" locale="US" />
                        </h:outputText>
                    </p:column>
                    <p:column styleClass="p-text-right" headerText="Germany">
                        <h:outputText value="#{product.price}">
                            <f:convertNumber type="currency" currencySymbol="" locale="DE" />
                        </h:outputText>
                    </p:column>

That the output would look at your global locale and only format any that match your currency. So in the above one column would be Currency for US if my locale was US and the Germany column would be Strings in the Excel Sheet.

@stolp
Copy link
Contributor

stolp commented Mar 25, 2021

Thanks, that clears this up!

@melloware melloware merged commit d265e24 into primefaces:master Apr 10, 2021
@melloware melloware deleted the PF7166 branch April 10, 2021 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataExporter: Excel export use currency data type for currency data
2 participants