How to get currency code from Excel using Apache P

2019-02-28 00:36发布

问题:

I'm using POI to parse Excel file and reg. expression to detect currency in NumericCellValue. I have 2 fields in Excel file with 2 difference currencies (100$ and 100€) and I need to get their currency code ("USD", "EUR").

case Cell.CELL_TYPE_NUMERIC: {
    Pattern p = Pattern.compile(currencyFilter);
    Matcher m = p.matcher(dataFormat);
    if (m.find()) {
         BigDecimal aCurrency = currentCell.getNumericCellValue();
         //I need to pass my currency code from cell field to money instance
         Money money = new Money(aCurrency, "USD");
    }
}

回答1:

String currencyCode = "";
if (dataFormat.contains("$$")) {
    currencyCode = "USD";
}
else if (dataFormat.contains("$€")) {
    currencyCode = "EUR";
}
moneyCurrency = new Money(bd, currencyCode);