Skip to content

Commit

Permalink
Fixes primefaces#8959 - DataExporter: Styles error on xls, xlsx and x…
Browse files Browse the repository at this point in the history
…lsxtream when using custom locale (primefaces#8962)
  • Loading branch information
jasonex7 committed Jul 6, 2022
1 parent e210cd5 commit 5137804
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
Expand Up @@ -448,7 +448,7 @@ protected void applyOptions(Workbook wb, DataTable table, Sheet sheet, ExporterO
currencyStyle = wb.createCellStyle();
currencyStyle.setFont(font);
currencyStyle.setAlignment(HorizontalAlignment.RIGHT);
String pattern = CurrencyValidator.getInstance().getPattern(locale);
String pattern = CurrencyValidator.getInstance().getExcelPattern(locale);
short currencyPattern = wb.getCreationHelper().createDataFormat().getFormat(pattern);
currencyStyle.setDataFormat(currencyPattern);
applyCellOptions(wb, options, currencyStyle);
Expand Down
Expand Up @@ -449,7 +449,7 @@ protected void applyOptions(Workbook wb, TreeTable table, Sheet sheet, ExporterO
currencyStyle = wb.createCellStyle();
currencyStyle.setFont(font);
currencyStyle.setAlignment(HorizontalAlignment.RIGHT);
String pattern = CurrencyValidator.getInstance().getPattern(locale);
String pattern = CurrencyValidator.getInstance().getExcelPattern(locale);
short currencyPattern = wb.getCreationHelper().createDataFormat().getFormat(pattern);
currencyStyle.setDataFormat(currencyPattern);
applyCellOptions(wb, options, currencyStyle);
Expand Down
Expand Up @@ -218,16 +218,16 @@ public DecimalFormat getFormat(Locale locale) {

/**
* <p>
* Returns a <code>String</code> representing the pattern for this currency.
* Returns a <code>String</code> representing the Excel pattern for this currency.
* </p>
*
* @param locale The locale a <code>NumberFormat</code> is required for, system default if null.
* @return The <code>String</code> pattern.
* @return The <code>String</code> pattern for using in Excel format.
*/
public String getPattern(Locale locale) {
public String getExcelPattern(Locale locale) {
DecimalFormat format = getFormat(locale);
String pattern = format.toLocalizedPattern();
pattern = pattern.replace(CURRENCY_SYMBOL_STR, format.getDecimalFormatSymbols().getCurrencySymbol());
pattern = pattern.replace(CURRENCY_SYMBOL_STR, "\"" + format.getDecimalFormatSymbols().getCurrencySymbol() + "\"");
String[] patterns = pattern.split(";");
return patterns[0];
}
Expand Down
Expand Up @@ -41,13 +41,16 @@ public class CurrencyValidatorTest {
private static String usDollar;
private static String ukPound;
private static String brazilReal;
private static String customCurrencySymbol;
private static Locale brazil = new Locale("pt", "BR");
private static Locale custom = new Locale("es", "US");

@BeforeAll
protected static void setUp() throws Exception {
usDollar = (new DecimalFormatSymbols(Locale.US)).getCurrencySymbol();
ukPound = (new DecimalFormatSymbols(Locale.UK)).getCurrencySymbol();
brazilReal = (new DecimalFormatSymbols(brazil)).getCurrencySymbol();
customCurrencySymbol = (new DecimalFormatSymbols(custom)).getCurrencySymbol();
}

private static int getVersion() {
Expand Down Expand Up @@ -132,11 +135,12 @@ public void testFromCurrency() {
* Test Patterns
*/
@Test
public void testPatterns() {
public void testExcelPatterns() {
CurrencyValidator validator = CurrencyValidator.getInstance();

assertEquals(usDollar + "#,##0.00", validator.getPattern(Locale.US), "US");
assertEquals(ukPound + "#,##0.00", validator.getPattern(Locale.UK), "UK");
assertEquals("\"" + usDollar + "\"" + "#,##0.00", validator.getExcelPattern(Locale.US), "US");
assertEquals("\"" + ukPound + "\"" + "#,##0.00", validator.getExcelPattern(Locale.UK), "UK");
assertEquals("\"" + customCurrencySymbol + "\"#,##0.00", validator.getExcelPattern(custom), "Custom");
}

/**
Expand Down

0 comments on commit 5137804

Please sign in to comment.