Reputation: 19
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.
Upvotes: 0
Views: 1461
Reputation: 109557
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;
Upvotes: 2