Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中读取Excel中的值,Windows与Linux之间的问题_Java_Excel_Linux_Windows_Apache Poi - Fatal编程技术网

在Java中读取Excel中的值,Windows与Linux之间的问题

在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

我在Springboot中有一个Java应用程序,我从单元格中读取数值3123456,89,我们在Excel中使用的格式是money European->
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();
}
...

另请参见。

谢谢您的回答,我将测试=)