2017-04-04 59 views
0

我使用POI来解析Excel文件和reg。表达式来检测货币NumericCellValue。我有2个差异货币(100 $和100€)Excel文件中的字段,我需要获得他们的货币代码(“美元”,“欧元”)。如何使用Apache POI从Excel中获取货币代码?

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

'XSSFCellStyle货币= cell.getCellStyle(); System.out.println(currency.getDataFormatString());'这应该返回格式为字符串。你也可以用'.getDataFormat()'把它缩短。在此之后,您可以与定义的值 – XtremeBaumer

+0

进行比较,它会将所有字段格式返回为[$$ - 409]#,## 0.00; [RED] \ - [$$ - 409]#,## 0.00但我该如何翻译此内容只为货币代码 –

+0

为美元字符串是:[$$ - 409]#,## 0.00,而欧元是:_- *#,## 0.00 [$€-1] _-; - *#,## 0.00 [$€-1] _-; _- *“ - ”?? [$€-1] _-; _- @ _-。正如你所看到的,美元只包含美元符号,而欧元包含欧元和美元符号。现在你可以检查字符串是否包含这些字符。有预定义的短裤,你也可以比较。这里是一个列表https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html – XtremeBaumer

回答

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