简体   繁体   中英

How to convert double value to int when data is passed from Excel

I've below code:

public class DataHelper {
     public static List<HashMap<String,String>> data(String filepath,String sheetName) {
          List<HashMap<String,String>> mydata = new ArrayList<>();
          try {
             FileInputStream fs = new FileInputStream(filepath);
             XSSFWorkbook workbook = new XSSFWorkbook(fs);
             XSSFSheet sheet = workbook.getSheet(sheetName);
             //System.out.println(sheet.getLastRowNum()+"total no of row");
             Row HeaderRow = sheet.getRow(0);
             //System.out.println(sheet.getPhysicalNumberOfRows()+"get the total no of rows");
             for(int i=1;i<sheet.getPhysicalNumberOfRows();i++) {
                 Row currentRow = sheet.getRow(i);
                 HashMap<String,String> currentHash = new HashMap<String,String>();
                 //System.out.println(currentRow.getPhysicalNumberOfCells());
                 for(int j=0;j<currentRow.getPhysicalNumberOfCells();j++) {
                     Cell currentCell = currentRow.getCell(j);
                     switch (currentCell.getCellType()) {
                         case Cell.CELL_TYPE_STRING:
                         //      
                             currentHash.put(HeaderRow.getCell(j).getStringCellValue(), currentCell.getStringCellValue());

                         case Cell.CELL_TYPE_Numeric:
                             currentHash.put(HeaderRow.getCell(j).getStringCellValue(), currentCell.getStringCellValue());
                             break;  
                     }
                 }
                 mydata.add(currentHash);
             }
             fs.close();
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
          return mydata;
       }

In the above code when integer value is passed from excel sheet it is converting it to double , as I m using hashmap which takes string arguments i cannot use integer.parseint. So how can i convert the double value to int?

Thanks.

If you do not want to rely on Excel/POI's format on getCellStringValue, do:

    break;
case Cell.CELL_TYPE_Numeric:
    double x = currentCell.getNumericCellValue();
    //String value = Double.toString(x);
    String value = Long.toString(Math.round(x));
    currentHash.put(HeaderRow.getCell(j).getStringCellValue(), value);
    break;

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