简体   繁体   中英

reading excel file using XMLStreamReader ( read number as string, not as a number )

I am reading an excel file using XMLStreamReader object. The initialization of this object is somewhat like this :

opcPkg = OPCPackage.open(excelPath, PackageAccess.READ);
xssfReader = new XSSFReader(opcPkg);
factory = XMLInputFactory.newInstance();
iterator = xssfReader.getSheetsData();
inputStream = iterator.next();
xmlReader = factory.createXMLStreamReader(inputStream);

The contents of a cell are read using this statement,

xmlReader.getElementText();

The problem is that a simple number string 10.2 contained in a cell is being read as 10.199999999999999. I want it to be read as 10.2 only. Is there a way to solve this ?

When you read it as a String, do you parse it with

Double.parseDouble(...getText() );

That would be the surest way to get a number out of a line of text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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