我有一個使用apache poi讀取xls表的應用程序。當單元格有數值時,我通過row.getCell(i).getNumericValue()讀取它。但它返回浮點數字。如單元格值爲1,則返回1.0。我可以將它轉換爲int嗎?任何幫助,將不勝感激。我試過的Integer.parseInt(值) - 但它引發的NumberFormat exception.Any幫助appreciated.Here是僞代碼:在Java中使用Apache POI讀取數字單元的整數
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
String value= String.valueOf(cell.getNumericCellValue());
int intVal = Integer.parseInt(value)-->>throws Exception
明白了,取值兼作conert爲int:雙VAL = cell.getNumericCellValue(); val.intValue將給出答案 – parameswar 2014-09-10 17:39:33
這裏有一個簡單的解決方案: 'double dval = row.getCell(cellCounter,XSSFRow.CREATE_NULL_AS_BLANK).getNumericCellValue(); (新Double(dval).intValue())。toString());' – 2016-08-19 14:22:11