简体   繁体   English

从Excel传递数据时如何将双精度值转换为int

[英]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. 在上面的代码中,当从excel工作表传递整数值时,它会将其转换为double,因为我使用带有字符串参数的hashmap,所以我不能使用integer.parseint。 So how can i convert the double value to int? 那么如何将double值转换为int?

Thanks. 谢谢。

If you do not want to rely on Excel/POI's format on getCellStringValue, do: 如果您不想依赖getCellStringValue上的Excel / POI格式,请执行以下操作:

    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;

暂无
暂无

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

相关问题 使用Java从Excel读取数据时,如何仅提取整数值而不提取Double值 - How to extract only the integer value instead of the Double value when reading data from Excel using Java (JAVA)如何将双精度从列表转换为整数 - (JAVA) How to convert a double from a list to an int 如何在 Java 中将 String 值转换为 Double 或 Int 值? - How to Convert String value into Double or Int value in Java? 将两个 Double 相除并将 Double 商乘以 Int 时如何将 Double 转换为 Int? - How to convert Double to Int when dividing two Doubles and multiplying the Double quotient by Int? 为什么在超出限制的 int 中滥用 1 而不是 1.0 会影响我们将其转换为 double 时获得的值 - Why does misusing 1 instead of 1.0 from an int exceeding limits effect the value we get when we convert it to a double 如何将数字从 double 转换为 int 而不舍入? - How to convert number from double to int without rounding? 当int是必要的时候,让程序不接受double值 - Keep program from accepting double value when int is necessary 无法从int []转换为double error Java - cannot convert from int[] to double error java 类型不匹配:无法从 double [] 转换为 int [] - Type mismatch: cannot convert from double [] to int [] 如何将每个整数/双精度值从包含嵌套对象和 arrays 的 JSON 数据转换为字符串? - How can I convert each integer/double value to String from JSON data containing nested objects and arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM