Java Operations Excel handles the accuracy loss of numeric types validation:
Scene:
Cell_type_numeric-->cell_type_string--->cell_type_numeric
POI version:
poi-3.10.1
poi-3.9
Code:
Packagepoi;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook; Public classDoublewithstringnumeric {Private Static FinalString excelname= "Doublewithstringnumberic.xls"; Public Static voidMain (string[] args)throwsIOException {intDouble_idx=0; intDouble2string_idx=1; intstring2double_idx=2; Workbook WB=NewHssfworkbook (); Sheet Sheet=wb.createsheet ("Doublewithstringnumberic"); Row Row=sheet.createrow (1); Cell Cell=Row.createcell (DOUBLE_IDX); Cell.setcelltype (Cell.cell_type_numeric); Cell.setcellvalue (99.333333); Persistworkbook (WB); Travelsheet (); System.out.println ("Type:cell_type_numeric==>cell_type_string"); InputStream s=NewFileInputStream (excelname); WB=NewHssfworkbook (s); Sheet=wb.getsheetat (0); Row=sheet.getrow (1); Cell=row.getcell (0); Double D=Cell.getnumericcellvalue (); Cell=Row.createcell (DOUBLE2STRING_IDX); Cell.setcelltype (cell.cell_type_string); Cell.setcellvalue (string.valueof (d)); Persistworkbook (WB); S.close (); Travelsheet (); System.out.println ("Type:cell_type_string==>cell_type_numeric"); S=NewFileInputStream (excelname); WB=NewHssfworkbook (s); Sheet=wb.getsheetat (0); Row=sheet.getrow (1); Cell=Row.getcell (DOUBLE2STRING_IDX); String double2string=Cell.getstringcellvalue (); Cell=Row.createcell (STRING2DOUBLE_IDX); Cell.setcelltype (Cell.cell_type_numeric); Cell.setcellvalue (Double.parsedouble (double2string)); Persistworkbook (WB); S.close (); Travelsheet (); } Private Static voidTravelsheet ()throwsFileNotFoundException, IOException {Workbook wb; Sheet Sheet; InputStream s=NewFileInputStream (excelname); WB=NewHssfworkbook (s); Sheet=wb.getsheetat (0); for(Row row_temp:sheet) { for(Cell cell_temp:row_temp) {getcellvalue (cell_temp); }} s.close (); } Private Static voidGetcellvalue (cell cell) {Switch(Cell.getcelltype ()) { CaseCell.CELL_TYPE_NUMERIC:System.out.println ("Cell_type_numeric:" +Cell.getnumericcellvalue ()); Break; CaseCell.CELL_TYPE_STRING:String Stringcellvalue=Cell.getstringcellvalue (); System.out.println ("Cell_type_string:" +stringcellvalue); System.out.println ("ToDouble:" +double.parsedouble (Stringcellvalue)); Break; default: System.out.println ("Error"); Break; } } Private Static voidPersistworkbook (Workbook WB)throwsFileNotFoundException, IOException {outputstream stream=NewFileOutputStream (excelname); Wb.write (stream); Stream.flush (); Stream.Close (); }}
Output:
cell_type_numeric:99.333333type:cell_type_numeric==>cell_type_stringcell_type_numeric: 99.333333cell_type_string:99.333333todouble:99.333333type:cell_type_string ==> cell_type_numericcell_type_numeric:99.333333cell_type_string:99.333333todouble: 99.333333cell_type_numeric:99.333333
Conclusion:
This scenario has no loss of precision.
Java Operations Excel handles the loss of precision for numeric types validation