[英]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.