zoukankan      html  css  js  c++  java
  • java操作Excel处理数字类型的精度损失问题验证

    java操作Excel处理数字类型的精度损失问题验证:

    场景:

    CELL_TYPE_NUMERIC-->CELL_TYPE_STRING--->CELL_TYPE_NUMERIC

    POI版本:
    poi-3.10.1
    poi-3.9

    Code:

    package poi;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    
    public class DoubleWithStringNumeric {
    
        private static final String excelName="DoubleWithStringNumberic.xls";
        public static void main(String[] args) throws IOException {
            int double_idx=0;
            int double2String_idx=1;
            int string2double_idx=2;
            Workbook wb=new HSSFWorkbook();
            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=new FileInputStream(excelName);
            wb=new HSSFWorkbook(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=new FileInputStream(excelName);
            wb=new HSSFWorkbook(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 void travelSheet() throws FileNotFoundException, IOException {
            Workbook wb;
            Sheet sheet;
            InputStream s=new FileInputStream(excelName);
            wb=new HSSFWorkbook(s);
            sheet=wb.getSheetAt(0);
            for (Row row_temp : sheet) {
                for (Cell cell_temp : row_temp) {
                    getCellValue(cell_temp);
                }
            }
            s.close();
        }
    
        private static void getCellValue(Cell cell) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.println("CELL_TYPE_NUMERIC:"+cell.getNumericCellValue());
                break;
            case Cell.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 void persistWorkbook(Workbook wb)
                throws FileNotFoundException, IOException {
            OutputStream stream=new FileOutputStream(excelName);
            wb.write(stream);
            stream.flush();
            stream.close();
        }
    
    }

    Output:

    CELL_TYPE_NUMERIC:99.333333
    Type: CELL_TYPE_NUMERIC==>CELL_TYPE_STRING
    CELL_TYPE_NUMERIC:99.333333
    CELL_TYPE_STRING:99.333333
    toDouble:99.333333
    Type: CELL_TYPE_STRING==>CELL_TYPE_NUMERIC
    CELL_TYPE_NUMERIC:99.333333
    CELL_TYPE_STRING:99.333333
    toDouble:99.333333
    CELL_TYPE_NUMERIC:99.333333

    结论:

    此场景无精度损失。

  • 相关阅读:
    4星|《激荡十年,水大鱼大》:过去十年间国内商业简史
    4星|《三联生活周刊》2017年47期:所谓“嬉皮精神”,说白了就是让每一个人都能在不影响其他人的前提下,过自己想要的生活
    3星|《三联生活周刊》2017年48期:联大外文系主任叶公超让学生念一句英语就能判断出学生的籍贯
    3星|《终身成长》:成长型思维让人进步,固定型思维让人固步自封。有新意的励志书,但有锤子模式的嫌疑。
    4星|《癌症新知:科学终结恐慌》:非常新鲜的癌症科普
    PL/SQL Developer使用技巧
    Oracle数据导入导出imp/exp sp2-0734:未知的命令开头'imp...解决方法
    oracle数据库导入导出命令!
    如何完全卸载VS2010
    Oracle命令(一):Oracle登录命令
  • 原文地址:https://www.cnblogs.com/softidea/p/4205595.html
Copyright © 2011-2022 走看看