2017-04-04 118 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);