在Java中读取Excel中的值,Windows与Linux之间的问题
我在Springboot中有一个Java应用程序,我从单元格中读取数值3123456,89,我们在Excel中使用的格式是money European->在Java中读取Excel中的值,Windows与Linux之间的问题,java,excel,linux,windows,apache-poi,Java,Excel,Linux,Windows,Apache Poi,我在Springboot中有一个Java应用程序,我从单元格中读取数值3123456,89,我们在Excel中使用的格式是money European->1000和,小数 String stringValue = new DataFormatter().formatCellValue(cell); 我以字符串的形式读取单元格 然后我将这个字符串解析为Double-> Double.parseDouble(stringValue .replaceAll("\\.", "").replaceAll
1000和,
小数
String stringValue = new DataFormatter().formatCellValue(cell);
我以字符串的形式读取单元格
然后我将这个字符串解析为Double->
Double.parseDouble(stringValue .replaceAll("\\.", "").replaceAll(",", "."));
我需要这个解析,因为我们是欧洲格式的插入数字,而Java是美国格式的(,千和,小数)
此过程适用于WINDOWS。
现在我上传我的应用程序到Linux,我有任何问题
现在我得到这个值->3123456,89
,如果我使用replace->“Error multiple points”
如果我想替换,
我会丢失部分小数
我使用apache.poi来阅读Excels
<!-- EXCELS -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
org.apache.poi
poi ooxml
3.15
最后,在linux中,我发现这段代码有问题,在windows中是正确的。谢谢。您的问题似乎是因为您的Linux
系统Java
使用了与Windows
系统不同的默认Locale
您可以构造具有特殊区域设置的数据格式化程序
。如果这是Locale.US
,则不需要整个替换,因为stringValue
将针对该Locale.US
进行格式化。然后,您可以使用相同区域设置的java.text.NumberFormat
从字符串中获取Number
...
DataFormatter dataFormatter = new DataFormatter(java.util.Locale.US);
java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance(java.util.Locale.US);
String stringValue = dataFormatter.formatCellValue(cell);
Double doubleValue = null;
try {
doubleValue = numberFormat.parse(stringValue).doubleValue();
} catch (Exception ex) {
//do nothing
}
...
另一种方法是将单元格值获取为数值,而不是格式化字符串
对于ApachePOI3.15
,这将类似于:
...
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
Double doubleValue = cell.getNumericCellValue();
}
...
另请参见。谢谢您的回答,我将测试=)