简体   繁体   English

使用Apache POI从excel文件中获取的数字

[英]Number as it is from excel file using Apache POI

I am using Apache POI (version 3.9) framework for dealing with excel files, ie reading excel file. 我正在使用Apache POI(3.9版)框架处理excel文件,即读取excel文件。

I am having trouble with number types. 我在数字类型方面遇到麻烦。 User can write a number in excel file with comma or dot as decimal separator, ie 12.34 or 12,34, for various reasons. 由于各种原因,用户可以在excel文件中用逗号或点作为小数点分隔符写一个数字,即12.34或12,34。

Now, I would like to get that value as it is (ie if it is 12.34, then I would like to get 12.34, or if it is 12,34, then I would like to get 12,34), but instead POI Cell preliminary gives me a double with dot as decimal separator. 现在,我想按原样获得该值(即,如果它是12.34,那么我想获得12.34,或者如果它是12,34,那么我想获得12,34),但是取而代之的是POI Cell初步给了我一个加双点的小数点分隔符。 So, if a cell value was 12,34, POI Cell would give double of value 12.34. 因此,如果像元值是12,34,则POI像元将给出值12.34的两倍。

This behaviour is not what I would like to be. 这种行为不是我想要的。 I would like to get the value that was entered (12,34). 我想获取输入的值(12,34)。

How to avoid/solve this? 如何避免/解决这个问题?

I have searched Stackoverflow for similar problems and tried to use solution given in thread How can I read numeric strings in Excel cells as string (not numbers) with Apache POI? 我已经在Stackoverflow中搜索了类似的问题,并尝试使用线程中给出的解决方案。 如何使用Apache POI将Excel单元格中的数字字符串读取为字符串(不是数字)? and also tried using alternative org.apache.poi.ss.usermodel.DataFormatter class, and it works, but it does not work when the the value is 12,34 and type of cell is Number defined in Excel file itself. 并且还尝试使用替代的org.apache.poi.ss.usermodel.DataFormatter类,它可以工作,但是当值是12,34并且单元格的类型是Excel文件本身中定义的Number时,它不起作用。

If you are validating the numbers, I would suggest that you might have more luck converting your validation string into a number to compare with the value in the cell. 如果您要验证数字,我建议您将验证字符串转换为数字以与单元格中的值进行比较可能会比较幸运。 There is no information stored in the spreadsheet about the decimal/thousands separator. 电子表格中没有存储有关十进制/千位分隔符的信息。 That is based on the locale set in the application or OS and is display-only - it is not stored at all. 它基于在应用程序或OS中设置的语言环境,并且仅显示-根本不存储。 So you will have to manually format the number as a String. 因此,您将必须手动将数字设置为字符串格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM