我的代码如下:
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;
}
在上面的代码中,当从excel表传递整数值时,它将它转换为double,因为我使用带有字符串参数的hashmap,我不能使用integer.parseint。那么如何将double值转换为int?
感谢。
答案 0 :(得分:1)
如果您不想依赖于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;